Python | 循环语句为excel文件中的每个工作表创build文件

我是相当新的python,并试图弄清楚如何我可以从excel工作簿中的每个工作表导出到每个工作表的不同文件的数据。

例如,在第一个工作簿中,每张表都有来自不同品牌的数据; 让我们把这个文件叫做allbrandsmasterlist excel文件。 我也有一个perbrandbreakdown文件,这是我希望每个品牌粘贴到每个品牌不同文件的模板。

总之,我想将每个品牌的所有内容都复制到一个新的finalbrand文件中,该文件保持perbrandbreakdown中的相同模板,但已自动将allbrandsmasterlist表中每个品牌的数据移动到其自己的文件中。 最终结果应该是第一个文件中的每个图纸/品牌的1个文件。

我已经取得了一些进展,但我需要帮助清理这个代码,并使其运行第一个文件中的所有工作表的循环,现在它只是运行一次,只创build第一个品牌的文件(只有文件输出品牌0或第一张)。 提前致谢,让我知道,如果我可以澄清任何混淆

这里是最新的变化

#import excel writing and reading modules import xlwt import xlrd file = 'i:/My Client Data/CLASSHOG FINAL TEMPLATES AND FILES/item master new august.xlsx' workbook1 = xlrd.open_workbook(file) print ('There are %s sheets in %s'% (workbook1.nsheets,file)) """for i in range(workbook.nsheets): print(i)""" for i in range(workbook1.nsheets): if workbook1.nsheets > 1: workbookwt = xlwt.Workbook() sheet1 = workbookwt.add_sheet('ITEM DETAILS') #open template file to copy from workbook = xlrd.open_workbook('I:\My Client Data\CLASSHOG FINAL TEMPLATES AND FILES/PERFORMANCE MACHINE.xlsx') #set current sheet to first one in workbook sheet = workbook.sheet_by_index(0) #declare contents of rows individually - manual datarow0 = [sheet.cell_value(0, col) for col in range(sheet.ncols)] datarow1 = [sheet.cell_value(1, col) for col in range(sheet.ncols)] datarow2 = [sheet.cell_value(2, col) for col in range(sheet.ncols)] datarow3 = [sheet.cell_value(3, col) for col in range(sheet.ncols)] datarow4 = [sheet.cell_value(4, col) for col in range(sheet.ncols)] #declare contents of columns individually - manual datacolA = [sheet.cell_value(row,0) for row in range(sheet.nrows)] datacolB = [sheet.cell_value(row,1) for row in range(sheet.nrows)] #number of worksheets, columns and rows respectively print("The number of worksheets is %s:" % (workbook.nsheets)) print("The number of columns is %s:" % (sheet.ncols)) print("The number of rows is %s:" % (sheet.nrows)) #add first sheet and paste columns-manual for index, value in enumerate(datarow0): sheet1.write(0, index, value) for index, value in enumerate(datarow1): sheet1.write(1, index, value) for index, value in enumerate(datarow2): sheet1.write(2, index, value) for index, value in enumerate(datarow3): sheet1.write(3, index, value) #---------------------------------------------------- sheet = workbookwt.add_sheet('CLEAN FILE') sheetswb1 = workbook1.sheet_by_index(i) datarow0 = [sheetswb1.cell_value(0, col) for col in range(sheetswb1.ncols)] datarow1 = [sheetswb1.cell_value(1, col) for col in range(sheetswb1.ncols)] for r in range(sheetswb1.nrows): for c in range(sheetswb1.ncols): sheet.write(r, c, sheetswb1.cell_value(r, c)) #for index, value in enumerate(datarow0): #sheet.write(0, index, value) #for index, value in enumerate(datarow1): #sheet.write(1, index, value) #---------------------------------------------------- sheet = workbookwt.add_sheet('SC FILE') sheet3 = workbook.sheet_by_index(2) #---------------------------------------------------- sheet = workbookwt.add_sheet('EBAY FILE') sheet4 = workbook.sheet_by_index(3) #---------------------------------------------------- sheet = workbookwt.add_sheet('PRICING STRATEGY') sheet5 = workbook.sheet_by_index(4) datarow0 = [sheet5.cell_value(0, col) for col in range(sheet5.ncols)] datarow1 = [sheet5.cell_value(1, col) for col in range(sheet5.ncols)] datarow2 = [sheet5.cell_value(2, col) for col in range(sheet5.ncols)] for index, value in enumerate(datarow0): sheet.write(0, index, value) for index, value in enumerate(datarow1): sheet.write(1, index, value) for index, value in enumerate(datarow2): sheet.write(2, index, value) workbookwt.save('i:/pythonvirioutput/output%s.xls' % ("brand" + str(i))) print('Operation run and file saved for brand' + str(i)) else: print('The workbook has less than 2 sheets') break 

产量

 There are 441 sheets in i:/My Client Data/CLASSHOG FINAL TEMPLATES AND FILES/item master new august.xlsx The number of worksheets is 5: The number of columns is 2: The number of rows is 5: Operation run and file saved for brand0 

是的,这似乎只是工作,因为你得到当前表 –

 sheet = workbook.sheet_by_index(0) 

你总是得到第一张纸,因此它总是只写出第一张纸上的数据,而不是使用i在这里,分别得到每张纸。 代码 –

 sheet = workbook.sheet_by_index(i)