pythonpandasopenpyxlexcel-writer-xlsx

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


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?


Solution

  • 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()