1 回答
TA贡献1812条经验 获得超5个赞
您可以使用 pythonopenpyxl模块来实现此目的。
from openpyxl import load_workbook
我不知道为什么你需要第三个文件。您可以将一个文件附加到另一个文件。首先,读取一个包含以下内容的文件:
first_file_wb = load_workbook(/file_path, read_only=True) # wb stands for workbook
for sheet_name in first_file_wb.sheetnames: # You can iterate through sheets with sheetnames
first_file_ws = first_file_wb[sheet_name] # ws stands for worksheet
# after this you have access to specific sheet.
for row in first_file_ws.iter_rows():
for cell in row:
print('%s: cell.value=%s' % (cell, cell.value)) # You can write this values to a dict sheet by sheet.
...
操作这些值后,您可以将其附加到其他文件。
second_file_wb = load_workbook(/file_path)
for sheet_name in second_file_wb.sheetnames:
second_file_ws = first_file_wb[sheet_name] # ws stands for worksheet
last_row = second_file_ws.max_row # This will give you last row number of the file
# At this point, you can append the values after that row.
for key,value in your_dict_from_first_file.items():
# I assume key is a counter to values but if it is not, you can create a counter here. Just be sure <key+max> gives first available row to append.
second_file_ws.cell(row=key+max, column={column from dict}, value={value})
# or
second_file_ws.cell(row=key+max, column={column_number_from_dict}).value = value
添加回答
举报