繁体   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.

我设法将每个 excel 文件中第一张纸的内容合并到一个新 excel 文件中的一张纸中,如下图所示:一张纸中的组合纸

我写了这段代码来实现这个:

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')

但我有两个问题:

  1. 如何删除具有“未命名”的单元格,如上图所示
  2. 如何从所有其他 Excel 文件中复制其他工作表(每个 Excel 文件中的第二个工作表,而不是第一个工作表),并将其放入一个 Excel 文件中,如在多个图片中显示的学生姓名和学生姓名。

一个 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.

我拥有的每个 Excel 文件看起来像这个单个 excel 文件

如果您想将所有数据收集在一个工作表中,您可以使用以下脚本:

  1. 将所有要处理的excel工作簿(即excel文件)放入一个文件夹(见变量paths )。

  2. 使用glob.glob获取该文件夹中所有工作簿的路径。

  3. 使用read_excel(path, sheet_name=None)返回每个工作簿的所有工作表并准备合并。

  4. 使用concat合并所有工作表。

  5. 导出最终的 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.

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