Home » excel » python – Convert a CSV/Excel file into and EXCEL file with formatting as table

python – Convert a CSV/Excel file into and EXCEL file with formatting as table

Posted by: admin April 23, 2020 Leave a comment

Questions:

Context:

I have a query that runs daily. I would like to email the query result data to stakeholders but I need it pre formatted for them as they may not have the skills required to format the data as a table. I have SSRS which is great for presenting the data but I still want the file emailed to them for filtering. I can generate CSV or Excel from the query and automate the email of the output but not without pre formatting the table first.

Problem:

I have a simple CSV/Excel file output from a query.

cust_id,cust_name
1,bishop
2,ripley

I want to convert this CSV/Excel output file, to a file that is pre-formatted as a sortable, filterable table with headers, and automate this process.

Excel formatted table image

Is this possible from python or some other server friendly code snippet? Either CSV >> Excel Formatted or Excel >> Excel Formatted. Both starting file types are fine in this instance.

Limitation:

I cannot install, update or import packages that are not part of stock python libraries.

How to&Answers:

Python’s xlsxwriter should have all the functionality you need.

Module page here: http://xlsxwriter.readthedocs.io/index.html

Example for table formatting here http://xlsxwriter.readthedocs.io/tutorial02.html

Example for adding auto filters here: http://xlsxwriter.readthedocs.io/example_autofilter.html

Upon reading the updated Limitations (can’t install modules on the server), it’s clear that using xlsxwriter without something like Anaconda is a no-go. Fallback solution unknown, I don’t know of any module that comes with the standard python installation that does what you need.