我是python的新手,我正在试图弄清楚如何将excel工作簿中每个工作表的数据导出到每个工作表的不同文件中。
例如,在第一个工作簿中,每个工作表都包含来自不同品牌的数据;让我的allbrandsmasterlist excel文件调用此文件。我还有一个perbrandbreakdown文件,这是我希望每个品牌都粘贴到的模板,但每个品牌都有不同的文件。
总之,我想将每个品牌的所有内容复制到一个新的finalbrand文件中,该文件保留perbrandbreakdown中的相同模板,但已自动将allbrandsmasterlist表中每个品牌的数据移动到其自己的文件中。对于第一个文件中的每个工作表/品牌,最终结果应为1个文件。
我已经取得了一些进展,但我需要帮助清理这些代码并让它为第一个文件中的所有工作表运行循环,现在它只运行一次,只为第一个品牌创建文件(仅限文件)输出是brand0或第一张)。在此先感谢,如果我能解决任何困惑,请告诉我
#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
答案 0 :(得分:1)
是的,这似乎只是起作用,因为你得到当前的表格 -
sheet = workbook.sheet_by_index(0)
您只获得第一张纸,因此它总是只写出第一张纸上的数据,您应该在这里使用i
来分别获取每张纸。代码 -
sheet = workbook.sheet_by_index(i)