I am using below code to export DataFrame to excel using pyexcelerate
:
from pyexcelerate import Workbook
'''
code to populate DataFrame
'''
excel = [df.columns] + list(df.values)
wb = Workbook()
wb.new_sheet('Sheet1', data=excel)
wb.save(os.path.join(os.getcwd(), folder_name, file_name))
Everything works fine. Now, I would like to set Excel table format using Pyexcelerate. But, could not find the documentation on how to do that.
May I know how to set styles with Pyexcelerate
?
I would like to stick with Pyexcelerate as project is developed with this library and also because of project constraint.
Are you referring to these styles? Unfortunately table styles aren't supported because they're a rather complicated feature with a lot of edge cases and it's more flexible to set the styles directly.
You mentioned that you have millions of records, styling every other row is actually quite performant. Cache the Style
object to avoid recreating it every iteration.
wb = Workbook()
ws = wb.new_sheet("sheet name")
style = Style(fill=Fill(background=Color(255,0,0,0)))
for row in range(1, len(rows) + 1, 2):
ws.set_row_style(row, style)
wb.save("output.xlsx")
Even with millions of rows, this should not add much to your export time.