Python Pandas - 遍历 Excel 文件的文件夹,将数据从每个 Excel 文件的 sheet 导出到它们自己的 .xlsx 文件中
Python Pandas - loop through folder of Excel files, export data from each Excel file's sheet into their own .xlsx file
我有一个包含 Excel 个文件的文件夹,其中许多文件包含 3-4 个标签的数据,我只希望将其作为单个 Excel 个文件。例如,假设我有一个包含三个选项卡的 Excel 文件:“employees”、“summary”和“data”。我希望由此创建 3 个新的 Excel 文件:employees.xlsx、summary.xlsx 和 data.xlsx.
我有代码可以遍历文件夹并识别所有选项卡,但我一直在努力弄清楚如何将数据从每个 sheet 单独导出到其自己的 Excel 文件中。我已经到了可以遍历文件夹、打开每个 Excel 文件并找到每个 sheet 的名称的地步。这是我目前所拥有的。
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)
我有一个包含 Excel 个文件的文件夹,其中许多文件包含 3-4 个标签的数据,我只希望将其作为单个 Excel 个文件。例如,假设我有一个包含三个选项卡的 Excel 文件:“employees”、“summary”和“data”。我希望由此创建 3 个新的 Excel 文件:employees.xlsx、summary.xlsx 和 data.xlsx.
我有代码可以遍历文件夹并识别所有选项卡,但我一直在努力弄清楚如何将数据从每个 sheet 单独导出到其自己的 Excel 文件中。我已经到了可以遍历文件夹、打开每个 Excel 文件并找到每个 sheet 的名称的地步。这是我目前所拥有的。
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)