How to write to an excel with multiple worksheets using "xlsxwriter" and not "openpyxl"?

191 Views Asked by At

I am looking to store some texts into an excel with multiple work-sheets. I tried to do it using openpyxl and I am able to achieve it but I am not able to do the same using xlsxwriter.

I cannot use openpyxl due to an IllegalCharacterException popping up when using it. I know ways to remove or escape these characters but I do not want to remove or escape these characters and want to store them as it is in my excel.

I can achieve the maintainence of characters using xlsxwriter but not able to store it in multiple worksheets. Is there any solution to this?

1

There are 1 best solutions below

0
Florian Bernard On BEST ANSWER

You can try something like this.

import pandas as pd

writer = pd.ExcelWriter("my_results.xlsx", engine="xlsxwriter")

for i, element in enumerate(elements):
   df = pd.DataFrame(element)
   df.to_excel(writer, sheet_name=f"element_{i}")
writer.save()