PANDA和GLOB:将文件夹中的所有xlsx文件转换为CSV类型错误:__init__()获得意外的关键字参数'xfid'

人气:395 发布:2022-10-16 标签: python csv glob pandas converters

问题描述

我有一个文件夹,其中包含许多要转换为CSV文件的xlsx文件。

在我的研究过程中,我发现了几个关于这个话题的帖子,比如this或that一个。基于此,我使用globpandas编写了以下代码:

import glob
import pandas as pd

path = r'/Users/.../xlsx files'
excel_files = glob.glob(path + '/*.xlsx')

for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel)         # error occurs here 
    df.to_csv(out)

但不幸的是,我收到了以下错误消息,我无法在此上下文中解释该消息,并且我无法确定如何解决此问题:

Traceback (most recent call last):
  File "<input>", line 11, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 1131, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_openpyxl.py", line 475, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 391, in __init__
    self.book = self.load_workbook(self.handles.handle)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_openpyxl.py", line 486, in load_workbook
    return load_workbook(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 317, in load_workbook
    reader.read()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 281, in read
    apply_stylesheet(self.archive, self.wb)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/styles/stylesheet.py", line 198, in apply_stylesheet
    stylesheet = Stylesheet.from_tree(node)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/styles/stylesheet.py", line 103, in from_tree
    return super(Stylesheet, cls).from_tree(node)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.py", line 87, in from_tree
    obj = desc.expected_type.from_tree(el)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.py", line 87, in from_tree
    obj = desc.expected_type.from_tree(el)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.py", line 103, in from_tree
    return cls(**attrib)
TypeError: __init__() got an unexpected keyword argument 'xfid'

有人知道如何修复这个问题吗?非常感谢您的帮助!

推荐答案

我在这里遇到了同样的问题。经过几个小时的思考和搜索,我意识到问题实际上是文件。我用MS Excel打开了它,并保存了下来。阿拉卡赞,问题解决了。

文件已下载,因此我认为这是安全错误,或者只是文件创建方式的错误。XD

编辑: 这不是安全问题,而是文件生成过程中出现的错误。正确的文件有两个kb错误的文件。 一种解决方案是:如果使用xlrd==1.2.0可以打开文件,则可以在打开文件后调用Read_EXCEL到Book(由xlrd打开的文件)。

import xlrd

# df = pd.read_excel('TabelaPrecos.xlsx')
# The line above is the same result

a = xlrd.open_workbook('TabelaPrecos.xlsx')
b = pd.read_excel(a)

454