[英]Python Pandas - loop through folder of Excel files, export data from each Excel file's sheet into their own .xlsx file
I have a folder of Excel files, many of which have 3-4 tabs worth of data that I just want as individual Excel files.我有一个 Excel 文件的文件夹,其中许多文件有 3-4 个标签的数据,我只想要单独的 Excel 文件。 For example, let's say I have an Excel file with three tabs: "employees", "summary", and "data".
例如,假设我有一个包含三个选项卡的 Excel 文件:“员工”、“摘要”和“数据”。 I would want this to create 3 new Excel files out of this: employees.xlsx, summary.xlsx, and data.xlsx.
我希望它从中创建 3 个新的 Excel 文件:employees.xlsx、summary.xlsx 和 data.xlsx。
I have code that will loop through a folder and identify all of the tabs, but I have struggling to figure out how to export data individually from each sheet into its own Excel file.我的代码将遍历文件夹并识别所有选项卡,但我一直在努力弄清楚如何将数据从每张工作表单独导出到自己的 Excel 文件中。 I have gotten to the point where I can loop through the folder, open each Excel file, and find the name of each sheet.
我已经到了可以遍历文件夹的地步,打开每个 Excel 文件,然后找到每个工作表的名称。 Here's what I have so far.
这是我到目前为止所拥有的。
import pandas as pd
import os
# filenames
files = os.listdir()
excel_names = list(filter(lambda f: f.endswith('.xlsx'), files))
excels = [pd.ExcelFile(name, engine='openpyxl') for name in excel_names]
sh = [x.sheet_names for x in excels] # I am getting all of the sheet names here
for s in sh:
for x in s:
#there is where I want to start exporting each sheet as its own spreadsheet
#df.to_excel("output.xlsx", header=False, index=False) #I want to eventually export it obviously, this is a placeholder
import pandas as pd
import glob
# get the file names using glob
# (this assumes that the files are in the current working directory)
excel_names = glob.glob('*.xlsx')
# iterate through the excel file names
for excel in excel_names:
# read the excel file with sheet_name as none
# this will create a dict
dfs = pd.read_excel(excel, sheet_name=None)
# iterate over the dict keys (which is the sheet name)
for key in dfs.keys():
# use f-strings (only available in python 3) to assign
# the new file name as the sheet_name
dfs[key].to_excel(f'{key}.xlsx', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.