0

I would like to create an excel file where one of the sheets is filled with calculated values and titles that are associated with them that would look like this :

Excel_example

In my case I have already inserted 2 previous worksheets that are files with a df using pandas.

My code goes as such :

writer = pd.ExcelWriter(Excel_file.xlsx", engine='xlsxwriter')
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
txt1 = str('TITLE 1')
txt2 = str('TITLE 2')
...
Value1 = 30
Value2 = 45
Value3 = 50

This works without a problem

I then tried adding the new sheet with the wanted information using 2 different ways

1 :

txt1.to_excel(writer, sheet_name='sheet3', startrow=0, startcol=0)
txt2.to_excel(writer, sheet_name='sheet3', startrow=1, startcol=0)
...
Value1.to_excel(writer, sheet_name='sheet3', startrow=1, startcol=1)
...

But this gave me this error message - AttributeError: 'str' object has no attribute 'to_excel'

2 :

worksheet = writer.sheets['sheet3']
worksheet.write(0, 0, txt1)
worksheet.write(1, 0, txt1)
...
worksheet.write(1, 1, Value1)
...

But this gave me this error message - KeyError: 'sheet3'

Mikel Alba
  • 25
  • 5
  • Case 1: as the error code says: the string variable (object) txt1 does not support the attribute: pandas.to_excel is an attribute for pandas data frames. You may want to look up how to work with pandas data frames while using them. Good luck! – Anonymous Jul 15 '22 at 20:51
  • Case 2: you may want to try with a search on the error message and you will find the answer too. Good luck! – Anonymous Jul 15 '22 at 20:58

0 Answers0