简体   繁体   中英

How to merge multiple xlsx files into one single xlsx file with Different sheets by openpyxl

I would like to create one master_xlsx file which would consist of many smaller xlsx files.

I have xlsx files:

  • users_general_inf.xlsx 在此处输入图像描述
  • users_dates_inf.xlsx 在此处输入图像描述
  • users_controls_inf.xlsx 在此处输入图像描述

I would like master_xlsx to contain all files in such a way that one xlsx file is one sheet with its sheet_name and data from the file preserved在此处输入图像描述

My code:

import openpyxl

# --- Creating the master excel
# ------ create the master excel workbook
wb_master = openpyxl.Workbook()
# ------ create the master excel worksheet
wb_master_sheet = wb_master.active
# ------ import .xlsx files
# --------- users_general_inf.xlsx
wb_01 = openpyxl.load_workbook('./tmp/users_general_inf.xlsx')
wb_01_sheet = wb_01['users_general_inf']
# --------- users_dates_inf.xlsx
wb_02 = openpyxl.load_workbook('./tmp/users_dates_inf.xlsx')
wb_02_sheet = wb_02['users_dates']
# --------- users_controls_inf.xlsx
wb_03 = openpyxl.load_workbook('./tmp/users_controls_inf.xlsx')
wb_03_sheet = wb_03['userAccountControl']


xlsx_files = [wb_01, wb_02, wb_03] # all xlsx files to be merged


for f in xlsx_files:
    ? - universal code independent of the number of xlsx files

# ------ saving the master excel workbook
wb_master.save('./reports/master_users_inf.xlsx')

As I am just learning PYTHON I will appreciate any help related to this project.

You're close, you just need to implement (inside your for loop) create_sheet to create a new sheet in the master file and then use iter_rows to grab all the values from all the rows.

Here is a proposition ( without your comments to make a short code ):

from openpyxl import load_workbook, Workbook

wb_master = Workbook()

xlsx_files = ['./tmp/users_general_inf.xlsx',
              './tmp/users_dates_inf.xlsx',
              './tmp/users_controls_inf.xlsx']

for f in xlsx_files:
    sub_wb = load_workbook(f)
    sub_ws = sub_wb.active
    
    wb_master_sheet = wb_master.create_sheet(sub_ws.title)
    
    for row in sub_ws.iter_rows():
        for cell in row:
            wb_master_sheet[cell.coordinate].value = cell.value
    
   #wb_master_sheet.column_dimensions["A"].width = 20 #uncomment this line to set a width    

wb_master.remove_sheet("Sheet") #or del wb_master["Sheet"] 
wb_master.save('./reports/master_users_inf.xlsx')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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