簡體   English   中英

如何從多個文件中提取相同的 excel 表?

[英]How can I extract the same excel sheet from multiple files?

我有一個類似 excel 文件的目錄,想從每個文件中提取第一張紙並將其保存為 .csv 文件。 目前有用於從單個文件中提取和保存工作表的代碼:

import glob
import pandas as pd

f = glob.glob('filename.xlsx') # assume the path
for excel in f:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel) # if only the first sheet is needed.
    df.to_csv(out) 

您可以使用帶有列表理解的 glob 將所有文件放入列表中:

files_to_be_read = glob.glob("*.xlsx") #Assuming you also have the path to the folder where the excel files are saved
for i in files_to_be_read:
    df_in = pd.read_excel(i) #You pass the path, pd.read_excel always uses the first sheet by default
    df_out = pd.to_csv(i+'.csv') #You will save the file with the same name, but in csv format

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM