如何解决在python中逐页和逐列附加多个Excel文件
请建议如何逐页和逐列附加多个excel文件,其中所有文件在python3中具有完全相同的表和列名称。
**Input** **Output**
File name: abc.xlsx File name: xyz.xlsx File name: appended.xlsx
Sheet: One Sheet: One Sheet: One
Column: A & B Column: A & B Column: A & B
Rows: 100 Rows: 100 Rows: **200**
. . .
. . .
. . .
. . .
. . .
. . .
Sheet: Ten Sheet: Ten Sheet: Ten
Column: A & B Column: A & B Column: A & B
Rows: 100 Rows: 100 Rows: **200**
解决方法
您可以为此使用python openpyxl
模块。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。