简体   繁体   English

Python Pandas - 循环浏览 Excel 文件的文件夹,将每个 ZC1D81AF583580.x 文件的数据导出到它们自己的文件“DED8FDCZ”中

[英]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.

相关问题 Python Pandas - 遍历文件夹 of.xlsx 文件,仅使用正则表达式从 Excel 选项卡中添加数据,名称中带有 xx.xx - Python Pandas - loop through folder of .xlsx files, only add data from Excel tabs with xx.xx in the name using regex Python:Excel 文件 (xlsx) 以变量为文件路径导出,使用 Pandas - Python: Excel file (xlsx) export with variable as the file path, using pandas 如何通过python中的pandas导出单个csv文件的多个excel表 - How to export to multiple excel sheet of a single csv file through pandas in python 遍历目录 for.xlsx 文件,将每个文件中的一张表中的数据附加到 dataframe - Looping through a directory for .xlsx files appending data from one sheet in each file to a dataframe Python - Excel 列大小调整,将多个工作表导出到 xlsx 文件 - Python - Excel Column sizing with multiple sheet export to xlsx files Python循环遍历excel文件并使用pandas read_excel函数一一读取 - Python Loop through excel file and and read each one by one using pandas read_excel function 如何使用Pandas将python Web抓取数据导出到现有excel文件中的特定工作表? - How can I export my python web scrape data to a specific sheet in an existing excel file using pandas? Python / Pandas:仅当每个Excel文件包含某些值时,才循环附加Excel文件 - Python/Pandas: Loop to append Excel files ONLY if each Excel file contains certain values Pandas-在文件夹中附加Excel文件,但也附加其各自的工作表,因此输出文件具有每个附加工作表 - Pandas- Append Excel files in folder, but also each of their respective sheets, so output file has each appended sheet 重命名文件夹中的文件,重命名的文件在excel表中 - Rename the files in a Folder, the file to renamed are in excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM