1

I'm pretty new in python and I need to do the following: in one xls file I need to export in 3 different worksheets 1. My data 2. The query that I used 3. A text

I use the following code:

import xlsxwriter
archive = 'mypath'+'file_name.xlsx'
my_query='select * from table_name'
df=data_frame_data
## Creating the wokrbook
workbook=xlsxwriter.Workbook(archive)
## naming the sheets
report_worksheet=workbook.add_worksheet('mydata')
report_worksheet.write(df, 'mydata')
sql_worksheet=workbook.add_worksheet('SQL')
sql_worksheet.write(my_query,'SQL')
txt_worksheet=workbook.add_worksheet('mytext')
txt_worksheet.write('this is my text','mytext')
workbook.close()

but it does not work any advice? note: all I can find is how to write multiple data frames in different worksheets

Chrissie M.
  • 121
  • 1
  • 7
  • You've found examples of how to write in different worksheets, but need help writing in multiple worksheets? What exactly is it that isn't working? Could [this](https://stackoverflow.com/questions/37522394/python-xlsxwriter-write-to-many-sheets) be of any help. – Torxed Sep 17 '19 at 10:18
  • The `worksheet.write` expects the row as first parameter. Documentation: https://xlsxwriter.readthedocs.io/worksheet.html – michael Sep 17 '19 at 10:20
  • thank you both for your answers. I can write a txt and the sql script im using in multiple worksheets but when it come to the data frame .write() does not work. I also tried writer=pd.ExcelWriter(mypath+file_name, engine='xlsxwriter') data_frame_data.to_excel( writer, sheet_name='my_report', index=False) but did not work – Chrissie M. Sep 17 '19 at 14:14

0 Answers0