我对 python 相当陌生,正在尝试弄清楚如何将 Excel 工作簿中每个工作表中的数据导出到每个工作表的不同文件中。
例如,在第一个工作簿中,每个工作表都有来自不同品牌的数据;我们将此文件称为我的 allbrandsmasterlist excel 文件。我还有一个 perbrandbreakdown 文件,这是我希望将每个品牌粘贴到其中的模板,但每个品牌都有不同的文件。
总之,我想将每个品牌的所有内容复制到一个新的 Finalbrand 文件中,该文件保留 perbrandbreakdown 中的相同模板,但已自动将 allbrandsmasterlist 表中每个品牌的数据移至其自己的文件中。最终结果应该是第一个文件中的每个工作表/品牌都有 1 个文件。
我已经取得了一些进展,但我需要帮助清理此代码并使其对第一个文件中的所有工作表运行循环,而现在它只运行一次并且只为第一个品牌创建文件(仅文件输出针对品牌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)
关于 python |循环语句为 Excel 文件中的每个工作表创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32109220/