简体   繁体   English

使用openpyxl读写多个excel数据到一个excel文件

[英]Read and Write multiple excel data into one excel file using openpyxl

I am trying to copy the data from multiple excel into one excel.我正在尝试将多个 excel 中的数据复制到一个 excel 中。 I am novice to python and openpyxl.我是 python 和 openpyxl 的新手。 So i have opened each file and went row by row and copied them.所以我打开了每个文件并逐行复制它们。 I want to do this with multiple files.我想对多个文件执行此操作。 How do i loop through row and columns and copy the data consider the column in all the files are same order?考虑到所有文件中的列顺序相同,我如何遍历行和列并复制数据?

import openpyxl as xl
from openpyxl import workbook

incident_wb = xl.load_workbook('incident resolved yesterday.xlsx')
incident_sheet = incident_wb['Page 1']

combined_wb = xl.Workbook()
combined_sheet = combined_wb.active
combined_sheet.title = "combined_sheet"
combined_wb.save('combined_sheet.xlsx')

for row in range(1, incident_sheet.max_row+1):
incident_no = incident_sheet.cell(row,1)
    opened_date = incident_sheet.cell(row,2)
    shrt_desc   = incident_sheet.cell(row,3)
    requester   = incident_sheet.cell(row,4)
    incdnt_type = incident_sheet.cell(row,5)
    priority    = incident_sheet.cell(row,6)
    assgn_grp   = incident_sheet.cell(row,7)
    assgn_to    = incident_sheet.cell(row,8)
    updated     = incident_sheet.cell(row,9)
    status      = incident_sheet.cell(row,10)
    sub_status  = incident_sheet.cell(row,11)
    ##copy the data into the new sheet
    incident_no_1 = combined_sheet.cell(row,1)
    incident_no_1.value = incident_no.value
    opened_date_1 = combined_sheet.cell(row,2)
    opened_date_1.value = opened_date.value
    shrt_desc_1 = combined_sheet.cell(row,3)
    shrt_desc_1.value = shrt_desc.value
    requester_1 = combined_sheet.cell(row,4)
    requester_1.value = requester.value
    incdnt_type_1 = combined_sheet.cell(row,5)
    incdnt_type_1.value = incdnt_type.value
    priority_1 = combined_sheet.cell(row,6)
    priority_1.value = priority.value
    assgn_grp_1 = combined_sheet.cell(row,7)
    assgn_grp_1.value = assgn_grp.value
    assgn_to_1 = combined_sheet.cell(row,8)
    assgn_to_1.value = assgn_to.value
    updated_1 = combined_sheet.cell(row,9)
    updated_1.value = updated.value
    status_1 = combined_sheet.cell(row,10)
    status_1.value = status.value
    sub_status_1 = combined_sheet.cell(row,11)
    sub_status_1.value = sub_status.value
    ##print(f"The incident resolved yesterday {incident_no.value}")

combined_wb.save('combined_sheet.xlsx')

An alternative approach would be to build a list of date from multiple excel files and then write it to another file.另一种方法是从多个 excel 文件构建日期列表,然后将其写入另一个文件。

As a proof of concept:作为概念证明:

import openpyxl as xl
from openpyxl import workbook

def provide_data(workbookName, sheetName):
    wb = xl.load_workbook(workbookName)
    sheet = wb[sheetName]
    return [[y.value for y in x] for x in sheet.iter_rows()] 
    # This creates an array of rows, which contain an array of cell values.
    # It will be much better to provide mapping for cells and return business object.

def save_data(list_of_sheets):
    combined_wb = xl.Workbook()
    combined_sheet = combined_wb.active
    combined_sheet.title = "combined_sheet"
    for sheet in list_of_sheets:
        for row in sheet:
            combined_sheet.append(row) # combining multiple rows.
    combined_wb.save('combined_sheet.xlsx')

workSheetsToCopy = [['incident resolved yesterday.xlsx', 'Page 1'], ['other.xlsx', 'Page 1']]

workSheetsToCopy = [provide_data(x[0], x[1]) for x in workSheetsToCopy]
save_data(workSheetsToCopy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM