I'm running into an issue that I think relates to needing to:
- export a dataframe to a new Excel worksheet (created at time of export)
- write specific values to an existing sheet in same workbook
- Doing both of the above in a loop
I can get 1 and 3 to work by themselves, and I can get 2 and 3 to work by themselves, but when I try to do all three things it doesn't work. I think there is some issue with the pandas to_excel using xlsxwriter engine conflicting with the sheets.write(row,column, value) to the same workbook.
For instance, this works by itself (note that I have the "writer" stuff to export dataframe to new sheet commented out):
import pandas as pd
import xlsxwriter
loopList = ["A","B","C","D","E"]
data = [['tom', 10], ['nick', 15], ['juli', 14]]
counter = 1
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Age'])
workbook = xlsxwriter.Workbook('C:\\Test\\Test.xlsx')
totalsSheet = workbook.add_worksheet('Totals')
writer = pd.ExcelWriter('C:\\Test\\Test.xlsx', engine = 'xlsxwriter')
for sheets in loopList:
#df.to_excel(writer, sheet_name = sheets, index=False)
totalsSheet.write(counter, counter, sheets + str(counter))
counter+=1
#writer.save()
#writer.close()
workbook.close()
The above makes the test.xlsx workbook, with a Totals worksheet, with "A1", "B2", etc. in incrementing row/column.
Likewise, when I comment out the workbook stuff and UN comment the pandas-export dataframe to new sheets, that also works:
import pandas as pd
import xlsxwriter
loopList = ["A","B","C","D","E"]
data = [['tom', 10], ['nick', 15], ['juli', 14]]
counter = 1
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Age'])
workbook = xlsxwriter.Workbook('C:\\Test\\Test.xlsx')
totalsSheet = workbook.add_worksheet('Totals')
writer = pd.ExcelWriter('C:\\Test\\Test.xlsx', engine = 'xlsxwriter')
for sheets in loopList:
df.to_excel(writer, sheet_name = sheets, index=False)
#totalsSheet.write(counter, counter, sheets + str(counter))
counter+=1
writer.save()
writer.close()
#workbook.close()
The above gives me a new Test workbook with 5 sheets (A, B, C, etc.) with the same dataframe exported to each.
However, I can't seem to do both; depending on the order in which I have the lines that write to Excel, it still only does one or the other (I don't get errors, I just get a result that's not both things I'm trying to do).
Is there a way to accomplish both of these things in the same loop?
I'm using python 3.x.x. Thanks for any help.