Generated chart in Excel file |
Reading and writing Excel file
There exist code libraries for many programming languages to read and write files in the Microsoft Excel format, usually in the XML-based "xlsx" format. For Python, a popular library is openpyxl. The documentation let's you get started with simple code samples. A spreadsheet consists of the workbook (consider it the file), one or more worksheets (where the tables and charts are), and some other elements, e.g., for styling. You can create a spreadsheet file with literally 10 lines of code.
Fetching data from Db2
To interface with Db2, I opted for SQLAlchemy and its core engine. I could have used one of the other Python drivers for Db2, but wanted to keep it as database-agnostic as possible.
After creating the SQLAlchemy engine and establishing a database connection, I executed the SQL SELECT statement, fetched the result set and added the rows to my data set for the spreadsheet.
Creating the spreadsheet and line chart
With openpyxl and a workbook and worksheet created, I added the rows from my result set.
Thereafter, I followed the code sample for a line chart with date axis. It configures a line chart, points to the added data for input, and adds the chart to the worksheet.
Then, I am done 😀:
Conclusions
Instead of exporting data from the database, then importing it into a spreadsheet, why not directly generate the spreadsheet and even add a chart? It can be easily done using one of the many existing code libraries.
If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik), Mastodon (@data_henrik@mastodon.social), or LinkedIn.