简体   繁体   English

Python,如何将不同的 excel 工作簿合并为一个 excel 工作簿作为工作簿

[英]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-sheet1emp-sheet2dept-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.

相关问题 有没有办法在python中将Excel工作簿组合成一个主工作簿? - Is there a way to combine Excel workbooks into one Master workbook within python? 将工作簿中的多个 Excel 工作表合并为一张工作表 Python - Combine Multiple Excel sheets within Workbook into one sheet Python 将来自不同excel文件的工作表连接到一个工作簿python中 - Joining sheets from different excel files into one workbook python 如何使用Python合并来自不同工作簿的多个同名excel工作表并保存到新的excel工作簿中 - How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python 如何使用 Python 将多个 CSV 文件合并为一个具有不同工作表的 Excel 文件 - How do I combine multiple CSV files into one excel files with different sheets using Python 如何使用python从多个excel工作簿中堆叠特定工作表 - how to stack specific sheets from multiple excel workbooks using python 将多个文本文件写入不同工作表上的一个excel工作簿? - Write multiple text files to one excel workbook on different sheets? 如何将excel工作簿中的多个工作表转换为python中的csv个文件 - How to convert multiple sheets in an excel workbook to csv files in python 使用 python 循环在一个 excel 工作簿中创建多个 excel 工作表,从一个 Z6A8064B5DF4794555500553C7 - Using python loop to create multiple excel sheets in one excel workbook from one dataframe Python:如何从一个 excel 文件中循环遍历多个工作表并将它们组合成一个 dataframe - Python: How to loop through multiple sheets from one excel file and combine them into one dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM