Python 从具有多个工作表的多个.xlxs文件中提取CSV文件[已回答]

Python 从具有多个工作表的多个.xlxs文件中提取CSV文件[已回答],python,excel,csv,export-to-csv,Python,Excel,Csv,Export To Csv,为了写我的论文《机械工程》,我收到了很多传感器数据,这些数据配置在多个excel文件(100)和多张表格(22)中。 现在我想将其可视化到power Bi中,但是.xlxs文件的运行速度会变慢,所以我希望所有数据(表)都在单独的CSV文件中。 我没有任何编程的实际经验,但将能够在jupyter或spyder中运行脚本 我在VBA中尝试了一个将多个EXCEL配置为csv的代码,但这只适用于.xlsx文件中的第一个工作表 我还在jupyter笔记本中使用了下面的代码;但这给了我一个excel的所有表

为了写我的论文《机械工程》,我收到了很多传感器数据,这些数据配置在多个excel文件(100)和多张表格(22)中。 现在我想将其可视化到power Bi中,但是.xlxs文件的运行速度会变慢,所以我希望所有数据(表)都在单独的CSV文件中。 我没有任何编程的实际经验,但将能够在jupyter或spyder中运行脚本

我在VBA中尝试了一个将多个EXCEL配置为csv的代码,但这只适用于.xlsx文件中的第一个工作表

我还在jupyter笔记本中使用了下面的代码;但这给了我一个excel的所有表格

data = pd.read_excel('file_name.file_format', sheet_name=None)

for sheet_name, df in data.items():
    df.to_csv(f'{sheet_name}.csv')

是否有人有用于此目的的代码,或者是否有人知道如何调整上面的代码,以便对文件夹中的所有excel文件执行此操作?

您可以尝试在每个包含.xlxs的文件的目录中循环,只需将您的目录替换为包含这些文件的文件夹的路径即可

我添加了“filename”,它只是一个没有扩展名的文件名,所以你可以将它添加到.csv文件名中

import os

directory = "\\YOUR_DIR\\HERE"
files = os.listdir(directory)

for xlxs_file in files:
    if ".xlxs" in xlxs_file:

        filename = xlxs_file.strip(".xlxs")
        xlxs_file = directory + "\\" + xlxs_file

        data = pd.read_excel(xlxs_file, sheet_name=None)
        for sheet_name, df in data.items():
            df.to_csv(f'{filename}_{sheet_name}.csv')

只要每个文件中的图纸名称相同,那么这应该是可行的:

import os
import pandas as pd

# target directory where the workbooks lie
tgt_dir = r'paste\directory\here\make\sure\to\keep\letter\r\before\quote'

# list of any files within the dir that have .xlsx in them
list_xl_files = [f for f in os.listdir(tgt_dir) if '.xlsx' in f.lower()]

# type a list of the sheets you want to target and extract
list_target_sheets = ['Sheet1', 'Sheet2', 'etc']

# iterate through each file and for each sheet in target sheets
for xl_file in list_xl_files:
    for sheet in list_target_sheets:
        
        # read in the file and target sheet
        df = pd.read_excel(tgt_dir+'\\'+xl_file, sheet_name=sheet)
        
        # export to csv but replace .xlsx with nothing 
        # then add _sheetname.csv so the filename shows the sheet too
        df.to_csv(tgt_dir+'\\'+xl_file.replace('.xlsx','')+'_'+sheet_name+'.csv')

不幸的是,有些文件有额外的传感器和数据,这意味着额外的工作表。但我现在有了这个代码:

import os
import pandas as pd

directory = "./"
files = os.listdir(directory)
for xlxs_file in files:
    if ".xlsx" in xlxs_file:
        filename = xlxs_file.strip(".xlsx")
        xlxs_file = os.path.join(directory, xlxs_file)
        data = pd.read_excel(xlxs_file, sheet_name=None)
        for sheet_name, df in data.items():
            df.to_csv("{}-{}.csv".format(filename, sheet_name))

为了澄清,您是说您需要另一个for循环,现有循环嵌套,外部循环扫描文件夹中的每个文件?正确,并且可能会将原始文件的名称添加到工作表中。不幸的是,它似乎不起作用,它只会生成文件夹中第一个文件的工作表folder@Tigerfire176真奇怪,它们都有相同的分机吗?你能把“打印(xlxs_文件)”放在xlxs_文件中的“if”.xlxs”下面吗:“看看它是否列出了文件?啊,我犯了一个错误-编辑了代码片段以进行更正我试图逐行运行它!”对于文件中的xlxs_文件:文件“”,文件中xlxs_文件的第1行:^SyntaxError:解析时出现意外EOF``对不起,不确定该建议什么-我使用了两本带有多张工作表的工作簿进行了尝试,得到了预期的结果。复制到SO后可能存在格式/缩进问题。听起来好像在“文件中的xlxs_文件”下找不到代码块:“不幸的是,有些文件有额外的传感器和数据,这意味着额外的工作表。但是我现在已经开始工作了:
import os import pandas as pd directory=“./”files=os.listdir(directory)for xlxs_file in files:if.xlsx in xlxs_file:filename=xlxs_file.strip(.xlsx”)xlxs_file=os.path.join(directory,xlxs_file)data=pd.read_excel(xlxs_file,sheet_name=None)for sheet_name,df in data.items():df.to_csv(“{}-{}.csv.”格式(文件名、工作表名称))
Jolly good。请发布您的答案,并标记此帖子的答案,以便其他人也可以在这里学习,而无需仔细阅读评论。干杯