亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Python中逐表和逐列追加多個Excel文件

在Python中逐表和逐列追加多個Excel文件

holdtom 2023-08-08 10:31:03
請建議如何逐個工作表和逐列附加多個 Excel 文件,其中所有文件在 python3 中具有完全相同的工作表和列名稱。                 **Input**                                **Output**File name: abc.xlsx       File name: xyz.xlsx       File name: appended.xlsxSheet: One                Sheet: One                Sheet: OneColumn: A & B             Column: A & B             Column: A & BRows: 100                 Rows: 100                 Rows: **200**.                         .                         ..                         .                         ..                         .                         ..                         .                         ..                         .                         ..                         .                         .Sheet: Ten                Sheet: Ten                Sheet: TenColumn: A & B             Column: A & B             Column: A & BRows: 100                 Rows: 100                 Rows: **200**
查看完整描述

1 回答

?
ABOUTYOU

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



查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號