How to merge multiple xlsx files into one single xlsx file with different sheets by openpyxl
Question:
I would like to merge several xlsx
files from list into one so that each sheet
has the name
of the next merged file. Single xlsx
files have one sheet
with data
stored in columns and rows. To create single files I am using openpyxl
library.
xlsx_files = ['u_gen_inf.xlsx', 'u_dates_inf.xlsx', 'u_ctrl_inf.xlsx']
loop I would like to create
from openpyxl import load_workbook, Workbook
for f in xlsx_files:
? load_workbook() - universal code independent of the number of xlsx files
wb_master.save('master_users_inf.xlsx')
Answers:
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 = ['u_gen_inf.xlsx', 'u_dates_inf.xlsx', 'u_ctrl_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('master_users_inf.xlsx')
I would like to merge several xlsx
files from list into one so that each sheet
has the name
of the next merged file. Single xlsx
files have one sheet
with data
stored in columns and rows. To create single files I am using openpyxl
library.
xlsx_files = ['u_gen_inf.xlsx', 'u_dates_inf.xlsx', 'u_ctrl_inf.xlsx']
loop I would like to create
from openpyxl import load_workbook, Workbook
for f in xlsx_files:
? load_workbook() - universal code independent of the number of xlsx files
wb_master.save('master_users_inf.xlsx')
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 = ['u_gen_inf.xlsx', 'u_dates_inf.xlsx', 'u_ctrl_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('master_users_inf.xlsx')