1

The goal of my program is to read two Excel files and compare them for differences. The whole program is run multiple times as it runs through a for loop (using fullList). I have achieved this and it works but I want to now be able to output the information in one Excel with each element of the list being on a new sheet. This is relevant code I have, when I open the Excel file after running there is no output. (workbook is included as well because in another part of the program I write strings to the same file, that part works)

fullList = ['a','b','c',...]
labels = ['a','b','c',...]

dfA = pd.read_csv('01.csv')
dfB = pd.read_csv('02.csv')

workbook = xlsxwriter.Workbook('NewExcelFile.xlsx')
worksheet = workbook.add_worksheet()

writer = pd.ExcelWriter('NewExcelFile.xlsx', engine='xlsxwriter')

marker = 0
for element in fullList:
    dfA_prod = dfA[dfA["Column in A"] == "%s" %(element)]
    dfB_prod = dfB[dfB["Column in B"] == "%s" %(element)]

    ...

    dfA_prod.to_excel(writer, sheet_name='%s' %(labels[marker+1]))
    dfB_prod.to_excel(writer, sheet_name='%s' %(labels[marker+1]))
Spiinner
  • 35
  • 5
  • See [Save list of DataFrames to multisheet Excel spreadsheet](https://stackoverflow.com/questions/14225676/save-list-of-dataframes-to-multisheet-excel-spreadsheet/51106608) – Brendan Jun 16 '21 at 19:53
  • Also note that you can use f-strings to format sheet names easily -- e.g., `f'{element}'` – Brendan Jun 16 '21 at 19:55
  • @Brendan I implemented the "with ExcelWriter(path) as writer:" from that link that they suggested and it didn't change anything. – Spiinner Jun 16 '21 at 20:02

0 Answers0