python:追加写入Excel 文件
以下是使用 openpyxl 库对 Excel 文件进行追加写入的示例代码:
from openpyxl import load_workbook
def append_to_excel(file_path, data):
wb = load_workbook(file_path)
ws = wb.active
# 找到最后一行
last_row = ws.max_row + 1
for row_data in data:
for col_index, cell_value in enumerate(row_data, start=1):
ws.cell(row=last_row, column=col_index, value=cell_value)
last_row += 1
wb.save(file_path)
# 示例用法
data_to_append = [['赵六', 23], ['孙七', 24]]
append_to_excel('your_excel_file.xlsx', data_to_append)