简体   繁体   English

如何调用文件夹中的所有 excel 文件并逐个运行 python 并将结果保存在一个 excel 文件中?

[英]How to call all the excel file in a folder and run through the pythons one by one and have the results save in one excel file?

So basically I have multiple excel files (different names) in a folder and I want to copy the same cell (for example B3) from all files and create a column in New excel file and put all the value there.所以基本上我在一个文件夹中有多个 excel 文件(不同的名称),我想从所有文件中复制相同的单元格(例如 B3)并在 New excel 文件中创建一个列并将所有值放在那里。

导入文件

The file above is what I want to import (multiple files like that).上面的文件是我要导入的(多个文件)。 I want to copy the names and emails and save it to the new file like the one below.我想复制姓名和电子邮件并将其保存到新文件中,如下所示。

导出新文件

So you want to read multiple files, get a specific cell and then create a new data frame and save it as a new Excel file:所以你想读取多个文件,获取一个特定的单元格,然后创建一个新的数据框并将其保存为一个新的 Excel 文件:

cells = []
for f in glob.glob("*.xlsx"):
    data = pd.read_excel(f, 'Sheet1')
    cells.append(data.iloc[3,5])
pd.Series(cells).to_excel('file.xlsx')

In my particular example I took cell F4 (row=3, col=5) - you can obviously take any other cell that you like or even more than one cell and then save it to a different list, combining the two lists in the end.在我的特定示例中,我采用了单元格 F4 (row=3, col=5) - 您显然可以选择您喜欢的任何其他单元格,甚至多个单元格,然后将其保存到不同的列表中,最后将两个列表组合起来. You could also have more complex logic where you could check one cell to decide which other cell to look at next.您还可以有更复杂的逻辑,您可以在其中检查一个单元格以决定接下来要查看哪个其他单元格。

The key point is that you want to iterate through a bunch of files and for each of them:关键是您要遍历一堆文件并针对每个文件:

  • read the file读取文件
  • extract whatever data you are interested in提取您感兴趣的任何数据
  • set this data aside somewhere把这些数据放在一边

Once you've gone through all the files combine all the data in any way that you like and then save it to disk in a format of your choice.浏览完所有文件后,以您喜欢的任何方式组合所有数据,然后以您选择的格式将其保存到磁盘。

暂无
暂无

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

相关问题 如何使用 python 依次编辑文件夹中的所有 excel 文件? - How to edit all excel file in a folder one after another with python? 如何将Excel工作簿中的所有工作表另存为一个文本文件? - How to save all worksheets in Excel workbook as one text file? 如果列没有标题,如何使用 python 分隔一列 CSV 文件,然后将其保存到新的 excel 文件中? - How to use python to seperate a one column CSV file if the columns have no headings, then save this into a new excel file? 如何读取文件夹中的所有文本文件内容并将这些内容作为行复制到一个Excel文件中 - How to read all text file contents in a folder and copy those contents as rows into one excel file Python:如何读取多个文件夹中的所有文本文件内容并将其保存到一个excel文件中 - Python: How to read all text file contents in multiple folders and save them into one excel file Python:如何将所有像素列表保存在一个 excel 文件中而不覆盖结果? - Python: how can I save all the lists of pixels in only one excel file without overwriting the outcomes? 如何将 append 多个 Excel 文件中的所有工作表放入一个 Excel 文件(不将它们合并或组合成一张工作表) - How to append all sheets in multiple Excel files into One Excel file (not consolidating or combining them into one sheet) 如何 web 将 excel 文件中存在的所有 URL 废弃在一个 go 中 - How to web scrap all the urls present in excel file in one go 如何在 AWS Glue 中将多个 Excel 文件编译并保存在一个 Excel 文件中 - How to compile and save multiple Excel files in one Excel file in AWS Glue 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