1

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.

Karthick Raju
  • 757
  • 8
  • 29
  • Perhaps https://github.com/kz26/PyExcelerate#styling-ranges –  May 28 '20 at 13:49
  • 1
    @Justin Ezequiel. Thanks for the url. I've already seen it. It's updating row style. The table I look at is having different color for alternate rows (Say, I look for Excel Format table style - medium 3). So, it may not work as expected. Also, I've millions of records to set style. Setting style by iterative will slow down performance. – Karthick Raju May 29 '20 at 03:45

1 Answers1

1

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.

kevmo314
  • 4,223
  • 4
  • 32
  • 43