I have some python code which creates a few pandas data frames which I need to put into a spreadsheet, with different data on multiple sheets and with slicers for easy filtering. This then gets sent out so others can easily see/filter the data. Currently, I manually copy and paste the updated tables into a template excel file, resave and send out, but I would like to automate this if possible.
My first thought was to use xlsxwriter
to write the formatting to generate this spreadsheet, however as per this answer, slicers aren't supported by the library, so that's no good.
I then considered seeing if I could automate opening a template excel document with the slicers already in place, delete the previous data in the tables and add in the new data. I found this question on using openpyxl
, and used some of the answers to try the below code to first remove the rows:
wb = openpyxl.load_workbook(r'TEST.xlsx')
ws = wb['sheet 1']
rows = ws.max_row
ws.delete_rows(13, rows)
wb.save(r'TEST2.xlsx')
However this removes the slicers and all formatting in the excel file completely, and not only saves a new copy but also removes the formatting from the original template as well, so that's no good either.
Does anyone know of a way I could automate creating or updating an excel file from python with slicers?
Edit: I'm not looking for recommendations on opinions on different libraries as the close vote suggests. I'm looking for a way to solve a specific coding problem, if it's possible, for which the code and research I've already tried has brought me up short.