简体   繁体   中英

Exporting data from DB2 using Python

We have nearly 300 reports which we download from the DB2 database on daily basis in.xlsx format and circulate to many recipients. This is done using the Data Transfer utility in the IBM Package. It's a very time consuming task to download each and every file using the Data Transfer utility. Is there a way I could download the data in.xlsx format by running a script in Python? All reports are already created so only requirement is to connect to the DB2 database library where the reports are saved and download the data in.xlsx format.

用于从 DB2 下载数据的实用程序

You could use the Python module pandas. There is a Stack Overflow answer showing how to load a Db2 result set into a pandas DataFrame . pandas has the built-in function to_excel() which allows to generale an xlsx file.

If you want to run the Python script on the Db2 server but download it to a local machine, you can take a look into my blog how to use the function to generate in-memory Excel or CSV files .

So this is the very condensed code with the query itself missing:

# read the Db2 result set into a DataFrame
df = pd.read_sql(sql, conn)
# write out to Excel file
df.to_excel('your_data.xslx', sheet_name='Sheet1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM