1

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?

Saleem Ali
  • 1,363
  • 11
  • 21
Ronnie
  • 483
  • 1
  • 5
  • 18
  • 1
    Possible duplicate of [Python XlsxWriter - Write to many sheets](https://stackoverflow.com/questions/37522394/python-xlsxwriter-write-to-many-sheets) – Florian Bernard Oct 21 '19 at 12:35
  • I have a unique data-frame for each excel work-sheet. How do I inculcate it into this? – Ronnie Oct 21 '19 at 12:58
  • @M Nikesh Could you please share some code, and the desired ouput ? – Florian Bernard Oct 21 '19 at 13:01
  • I will you the pseudo code -- 1. for loop which generates a dataframe for each iteration. each dataframe needs to be stored in a different excel worksheet. – Ronnie Oct 21 '19 at 13:23
  • If you are looking to add data or worksheets to an existing xlsx file then you can't do that with XlsxWriter. What are the characters that cause the `IllegalCharacterException` error? It seems odd that XlsxWriter can handle them and OpenPyXL can't. – jmcnamara Oct 21 '19 at 13:24
  • I am trying to create an excel with multiple worksheets using "xlsxwriter" – Ronnie Oct 21 '19 at 13:29
  • for i in range(list1): – Ronnie Oct 21 '19 at 13:37
  • 1
    Ok. In that case that is possible. You just need to do multiple calls to `add_worksheet()`. – jmcnamara Oct 21 '19 at 14:51

1 Answers1

1

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()
Florian Bernard
  • 2,561
  • 1
  • 9
  • 22