简体   繁体   English

使用 python 从 excel 中提取数据并写入一个空的 excel 文件

[英]Extracting data from excel using python and writing to an empty excel file

I have a large set of data that I am trying to extract from multiple excel files that have multiple sheets using python and then write that data into a new excel file.我有大量数据,我试图从多个 excel 文件中提取,这些文件有多个使用 python 的工作表,然后将该数据写入新的 excel 文件。 I am new with python and have tried to use various tutorials to come up with code that can help me automate the process.我是 python 的新手,并尝试使用各种教程来提供可以帮助我自动化该过程的代码。 However, I have reached a point where I am stuck and need some guidance on how to write the data that I extract to a new excel file.但是,我已经到了一个被卡住的地步,需要一些关于如何将我提取的数据写入新的 excel 文件的指导。 If someone could point me in the write direction, it would be greatly appreciated.如果有人能指出我的写作方向,将不胜感激。 See code below:请参见下面的代码:

import os
from pandas.core.frame import DataFrame
path = r"Path where all excel files are located"
os.chdir(path)
for WorkingFile in os.listdir(path):
   if os.path.isfile(WorkingFile):
      DataFrame = pd.read_excel(WorkingFile, sheet_name = None, header = 12, skipfooter = 54)
DataFrame.to_excel(r'Empty excel file where to write all the extracted data')       

When I execute the code I get an error "AttributeError: 'dict' object has no attribute 'to_excel'. So I am not sure how to rectify this error, any help would be appreciated.当我执行代码时,我收到一个错误“AttributeError:'dict' ZA8CFDE6331B59EB2AC96F8911C4B666Z 没有属性'to_excel'。所以我不知道如何纠正这个错误,任何帮助将不胜感激。

Little bit more background on what I am trying to do.关于我正在尝试做的事情的更多背景。 I have a folder with about 50 excel files, each file might have multiple sheets.我有一个包含大约 50 个 excel 文件的文件夹,每个文件可能有多个工作表。 The data I need is located on a table that consists of one row and 14 columns and is in the same location on each file and each sheet.我需要的数据位于一个由一行和 14 列组成的表上,并且位于每个文件和每个工作表的相同位置。 I need to pull that data and compile it into a single excel file.我需要提取该数据并将其编译为单个 excel 文件。 When I run the code above and and a print statement, it is showing me the exact data I want but when I try to write it to excel it doesn't work.当我运行上面的代码和打印语句时,它向我显示了我想要的确切数据,但是当我尝试将其写入 excel 时它不起作用。

Thanks for help in advance!提前感谢您的帮助!

Not sure why you're importing DataFrame instead of pandas.不知道为什么要导入 DataFrame 而不是 pandas。 Looks like your code is incomplete.看起来你的代码不完整。 Below code will clear the doubts you have.下面的代码将清除您的疑虑。 (Not include any conditions for excluding non excel files/dir etc ) (不包括排除非 excel 文件/目录等的任何条件)

import pandas as pd
import os

path = "Dir path to excel files" #Path

df = pd.DataFrame() # Initialize empty df

for file in os.listdir(path):

     data = pd.read_excel(path + file) # Read each file from dir
     df = df.append(data, ignore_index=True) # and append to empty df


# process df

df.to_excel("path/file.xlsx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM