将复制的数据保存到不同的 excel 个文件
Save copied data to different excel files
我试图将复制数据的不同部分从一个 excel 文件保存到相应的新 excel 文件,但我将所有内容都放在一个新文件中。
我做错了什么?
例如,在图片中我想有 4 个新的 excel 文件,名称为 AAA、BBB、CCC 和 DDD。
分隔是每次出现空白行时。
要复制的数据将从该行开始直到下一个空白行。 (所有列)
谢谢
wb1 = xl.load_workbook(xref_file)
ws1 = wb1.worksheets[0]
mr = ws1.max_row
mc = ws1.max_column
for row in range(1,ws1.max_row):
if(ws1.cell(row,1).value is None):
wb2 = openpyxl.Workbook()
ws2 = wb2.active
conveyor_name = ws1["A1"].value
conveyor_name.split(' -')
conveyor_name = conveyor_name.split(' -')[0]
filename = conveyor_name + ".xlsx"
destination_file = os.path.join(destination,filename)
wb2.save(destination_file)
# copying the cell values from source
# excel file to destination excel file
for i in range (1, mr+1):
for j in range (1, mc+1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i, column = j).value = c.value
wb2.save(destination_file)
ws1.delete_rows(1,mr)
wb1.save(xref_file)
编辑 1:
我对您的代码所做的更改是我有额外的条件来检查是否存在空行,如果它是空行,那么它会将 new_sheet 变量设置为 true。如果它不为空且 new_sheet 为真,则创建一个新的 sheet,否则将开始一个循环,将内容复制到新的 sheet。
因此更新后的代码应该如下:
wb1 = openpyxl.load_workbook(destination + "/" + xref_file)
ws1 = wb1.worksheets[0]
mr = ws1.max_row
mc = ws1.max_column
new_sheet = True
# for row in range(1,ws1.max_row):
row = 1
while row < ws1.max_row:
if(ws1.cell(row,1).value is not None):
if new_sheet == True:
wb2 = openpyxl.Workbook()
ws2 = wb2.active
conveyor_name = ws1["A" + str(row)].value
conveyor_name = conveyor_name.split()[0]
filename = conveyor_name + ".xlsx"
destination_file = os.path.join(destination,filename)
print(destination_file)
wb2.save(destination_file)
new_sheet = False
row = row + 1
else:
# copying the cell values from source
# excel file to destination excel file
for i in range (row, mr+1):
if ws1.cell(i,1).value is None:
break
for j in range (1, mc+1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i - row + 1, column = j).value = c.value
row = i
wb2.save(destination_file)
else:
new_sheet = True
row = row + 1
编辑 0:可以观察到一些优化和错误。由于我不知道 excel sheet 中数据的性质,因此其中一些可能不适用于您
wb1 = xl.load_workbook(xref_file) ws1 = wb1.worksheets[0]
mr = ws1.max_row
mc = ws1.max_column
for row in range(1,ws1.max_row):
if(ws1.cell(row,1).value is None):
wb2 = openpyxl.Workbook()
ws2 = wb2.active
- 如果上述 If 条件为真,则 A1 中不应有任何值。此外,如果 A1 中应该有一个值,那么您可能需要检查第二行中的值以通过 if 条件,因此该行的值应该从 2 到 ws1.max_row
conveyor_name = ws1["A1"].value
- 下面这行没有多大意义,因为您在之后的行中再次使用了相同的值
conveyor_name.split(' -') # this is not required
conveyor_name = conveyor_name.split(' -')[0]
filename = conveyor_name + ".xlsx"
destination_file = os.path.join(destination,filename)
wb2.save(destination_file)
# copying the cell values from source
# excel file to destination excel file
for i in range (1, mr+1):
for j in range (1, mc+1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i, column = j).value = c.value
wb2.save(destination_file)
ws1.delete_rows(1,mr) wb1.save(xref_file)
我试图将复制数据的不同部分从一个 excel 文件保存到相应的新 excel 文件,但我将所有内容都放在一个新文件中。
我做错了什么?
例如,在图片中我想有 4 个新的 excel 文件,名称为 AAA、BBB、CCC 和 DDD。 分隔是每次出现空白行时。 要复制的数据将从该行开始直到下一个空白行。 (所有列)
谢谢
wb1 = xl.load_workbook(xref_file)
ws1 = wb1.worksheets[0]
mr = ws1.max_row
mc = ws1.max_column
for row in range(1,ws1.max_row):
if(ws1.cell(row,1).value is None):
wb2 = openpyxl.Workbook()
ws2 = wb2.active
conveyor_name = ws1["A1"].value
conveyor_name.split(' -')
conveyor_name = conveyor_name.split(' -')[0]
filename = conveyor_name + ".xlsx"
destination_file = os.path.join(destination,filename)
wb2.save(destination_file)
# copying the cell values from source
# excel file to destination excel file
for i in range (1, mr+1):
for j in range (1, mc+1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i, column = j).value = c.value
wb2.save(destination_file)
ws1.delete_rows(1,mr)
wb1.save(xref_file)
编辑 1: 我对您的代码所做的更改是我有额外的条件来检查是否存在空行,如果它是空行,那么它会将 new_sheet 变量设置为 true。如果它不为空且 new_sheet 为真,则创建一个新的 sheet,否则将开始一个循环,将内容复制到新的 sheet。 因此更新后的代码应该如下:
wb1 = openpyxl.load_workbook(destination + "/" + xref_file)
ws1 = wb1.worksheets[0]
mr = ws1.max_row
mc = ws1.max_column
new_sheet = True
# for row in range(1,ws1.max_row):
row = 1
while row < ws1.max_row:
if(ws1.cell(row,1).value is not None):
if new_sheet == True:
wb2 = openpyxl.Workbook()
ws2 = wb2.active
conveyor_name = ws1["A" + str(row)].value
conveyor_name = conveyor_name.split()[0]
filename = conveyor_name + ".xlsx"
destination_file = os.path.join(destination,filename)
print(destination_file)
wb2.save(destination_file)
new_sheet = False
row = row + 1
else:
# copying the cell values from source
# excel file to destination excel file
for i in range (row, mr+1):
if ws1.cell(i,1).value is None:
break
for j in range (1, mc+1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i - row + 1, column = j).value = c.value
row = i
wb2.save(destination_file)
else:
new_sheet = True
row = row + 1
编辑 0:可以观察到一些优化和错误。由于我不知道 excel sheet 中数据的性质,因此其中一些可能不适用于您
wb1 = xl.load_workbook(xref_file) ws1 = wb1.worksheets[0] mr = ws1.max_row mc = ws1.max_column for row in range(1,ws1.max_row): if(ws1.cell(row,1).value is None): wb2 = openpyxl.Workbook() ws2 = wb2.active
- 如果上述 If 条件为真,则 A1 中不应有任何值。此外,如果 A1 中应该有一个值,那么您可能需要检查第二行中的值以通过 if 条件,因此该行的值应该从 2 到 ws1.max_row
conveyor_name = ws1["A1"].value
- 下面这行没有多大意义,因为您在之后的行中再次使用了相同的值
conveyor_name.split(' -') # this is not required conveyor_name = conveyor_name.split(' -')[0] filename = conveyor_name + ".xlsx" destination_file = os.path.join(destination,filename) wb2.save(destination_file) # copying the cell values from source # excel file to destination excel file for i in range (1, mr+1): for j in range (1, mc+1): # reading cell value from source excel file c = ws1.cell(row = i, column = j) # writing the read value to destination excel file ws2.cell(row = i, column = j).value = c.value wb2.save(destination_file) ws1.delete_rows(1,mr) wb1.save(xref_file)