简体   繁体   English

Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files

[英]Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files

It's my first time to use pandas, I have multiple excel files, that i want to combine all into one Excel file using python pandas. It's my first time to use pandas, I have multiple excel files, that i want to combine all into one Excel file using python pandas.

I managed to merge the content of the first sheets in each excel file into one sheet in a new excel file like this shown in the figure below: combined sheets in one sheet我设法将每个 excel 文件中第一张纸的内容合并到一个新 excel 文件中的一张纸中,如下图所示:一张纸中的组合纸

I wrote this code to implement this:我写了这段代码来实现这个:

import glob
import pandas as pd
path = "C:/folder"
file_identifier = "*.xls"
all_data = pd.DataFrame()
for f in glob.glob(path + "/*" + file_identifier):
   df = pd.read_excel(f)
   all_data = all_data.append(df,ignore_index=True)

writer = pd.ExcelWriter('combined.xls', engine='xlsxwriter')    
all_data.to_excel(writer, sheet_name='Summary Sheet') 
writer.save()
file_df = pd.read_excel("C:/folder/combined.xls")
# Keep only FIRST record from set of duplicates
file_df_first_record = file_df.drop_duplicates(subset=["Test summary", "Unnamed: 1", "Unnamed: 2", 
"Unnamed: 3"], keep="first")
file_df_first_record.to_excel("filtered.xls", index=False, sheet_name='Summary Sheet')

But I have two issues:但我有两个问题:

  1. How to remove cells that has "Unnamed" as shown in the previous figure如何删除具有“未命名”的单元格,如上图所示
  2. How to copy other worksheets (the second worksheet in each Excel file, not the first worksheet) from all other Excel files and put it in one Excel file with multiple worksheets and with different students names like shown in the picture.如何从所有其他 Excel 文件中复制其他工作表(每个 Excel 文件中的第二个工作表,而不是第一个工作表),并将其放入一个 Excel 文件中,如在多个图片中显示的学生姓名和学生姓名。

all worksheets in one excel file一个 excel 文件中的所有工作表

So i managed to combine worksheet1 from all Excel files in one sheet, but now I want to copy A, B, C, D, E worksheets into one Excel file that has all other remaining worksheets in other Excel files. So i managed to combine worksheet1 from all Excel files in one sheet, but now I want to copy A, B, C, D, E worksheets into one Excel file that has all other remaining worksheets in other Excel files.

Each Excel file of the ones I have looks like this single excel file我拥有的每个 Excel 文件看起来像这个单个 excel 文件

If you want to have all data gathered together in one worksheet you can use the following script:如果您想将所有数据收集在一个工作表中,您可以使用以下脚本:

  1. Put all excel workbooks (ie excel files) to be processed into a folder (see variable paths ).将所有要处理的excel工作簿(即excel文件)放入一个文件夹(见变量paths )。

  2. Get the paths of all workbooks in that folder using glob.glob .使用glob.glob获取该文件夹中所有工作簿的路径。

  3. Return all worksheets of each workbook with read_excel(path, sheet_name=None) and prepare them for merging.使用read_excel(path, sheet_name=None)返回每个工作簿的所有工作表并准备合并。

  4. Merge all worksheets with concat .使用concat合并所有工作表。

  5. Export the final output to_excel .导出最终的 output to_excel

     import pandas as pd import glob paths = glob.glob(r"C:\excelfiles\*.xlsx") path_save = r"finished.xlsx" df_lst = [pd.read_excel(path, sheet_name=None).values() for path in paths] df_lst = [y.transpose().reset_index().transpose() for x in df_lst for y in x] df_result = pd.concat(df_lst, ignore_index=True) df_result.to_excel(path_save, index=False, header=False)

暂无
暂无

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

相关问题 如何使用python将多个Excel文件中的数据合并到一个Excel文件中? - How can I use python to combine data from multiple excel files into one excel file? 使用python将Excel选项卡(工作表)从单个xls文件复制到单个目标xls文件 - Copy Excel tabs (worksheets) from individual xls files to single target xls file using python 使用python将Concat Excel文件和工作表合二为一 - Concat excel files and worksheets into one using python 如何使用 python 将 Excel 文件中的所有工作表合并或合并到一个工作表中? - How can combine or merge all worksheets within an Excel file into one worksheet using python? 通过python将多个excel文件合并成一个excel文件 - Join multiple excel files into one excel file via python 将特定行从多个文本文件复制到Excel文件 - Copy specific lines from multiple text files to an excel file 将数据从多个 excel 文件复制到特定工作表上的现有文件 - Copy Data from multiple excel files to existing file on specific sheet 在Python中将excel工作表从一个工作表复制到另一个工作表 - Copy excel sheet from one worksheet to another in Python 如何使用 python 将一个 excel 工作表模板复制到多个 excel 文件中 - How to copy one excel sheet template into multiple excel files using python 如何将 append 多个 Excel 文件中的所有工作表放入一个 Excel 文件(不将它们合并或组合成一张工作表) - How to append all sheets in multiple Excel files into One Excel file (not consolidating or combining them into one sheet)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM