[英]Python, how to combine different excel workbooks into one excel workbook as sheets
is there any way in python by which we can combine different excel workbooks into one excel workbook having sheets containing data of those different excel workbooks? is there any way in python by which we can combine different excel workbooks into one excel workbook having sheets containing data of those different excel workbooks?
For example lets say I am having two excel workbooks 1) emp.xlsx and 2) dept.xlsx i want output as output.xlsx (having worksheets as emp and dept with data of emp.xlsx and dept.xlsx). For example lets say I am having two excel workbooks 1) emp.xlsx and 2) dept.xlsx i want output as output.xlsx (having worksheets as emp and dept with data of emp.xlsx and dept.xlsx). Request you to please share your thoughts on this.
请您分享您对此的看法。
Regards Kawaljeet问候卡瓦吉特
What you need to do is get each sheet one by one and then create an excel with each one of those sheets.您需要做的是一张一张地获取每张纸,然后为每一张纸创建一个 excel。 You can use the file name to name the new sheets as in emp-sheet1 , emp-sheet2 , dept-sheet1 , and so on.
您可以使用文件名来命名新工作表,如emp-sheet1 、 emp-sheet2 、 dept-sheet1等。
The nest example assumes you have two excel files named emp.xlsx and dept.xlsx and generates a new output.xlsx file containing all the sheets and values:嵌套示例假设您有两个名为 emp.xlsx 和 dept.xlsx 的 excel 文件,并生成一个包含所有工作表和值的新 output.xlsx 文件:
#!pip install openpyxl
#!pip install xlrd
import pandas as pd
def get_sheets(filenames):
'''
This function generates dataframes from excel sheets.
Returns:
- dfs: a list of dataframes one for each sheet
- sheets: combined names for the new sheets filename+-+sheetname
'''
sheets = []
dfs = []
for file in filenames:
xl = pd.ExcelFile(file)
sheet_names = xl.sheet_names
for sheet in sheet_names:
dfs.append(xl.parse(sheet, header=None))
sheets.append(file.split('.')[0]+'-'+sheet)
return dfs, sheets
def save_xls(dfs, sheets, xls_path):
'''
Saves each dataframe in dfs as a sheet with the name in sheets
into the file specified in xls_path
'''
with pd.ExcelWriter(xls_path) as writer:
for n, df in enumerate(dfs):
df.to_excel(writer, sheets[n], index = False, header = None)
writer.save()
filenames = ['emp.xlsx', 'dept.xlsx']
dfs, sheets = get_sheets(filenames)
save_xls(dfs, sheets, 'output.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.