【问题标题】:python openpyxl.load_workbook TypeError: Fill() takes no argumentspython openpyxl.load_workbook TypeError: Fill() 不带参数
【发布时间】:2020-06-29 21:19:08
【问题描述】:

我想从 Excel 表中读取背景颜色。我正在用openpyxl尝试这个,但是当我尝试加载文件时 openpyxl.load_workbook("myfile.xlsx", data_only=True) 我收到以下错误:

Traceback (most recent call last):
  File "\openpyxl\descriptors\base.py", line 55, in _convert
    value = expected_type(value)
TypeError: Fill() takes no arguments

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "\openpyxl\reader\excel.py", line 315, in load_workbook
    reader.read()
  File "\openpyxl\reader\excel.py", line 279, in read
    apply_stylesheet(self.archive, self.wb)
  File "\openpyxl\styles\stylesheet.py", line 192, in apply_stylesheet
    stylesheet = Stylesheet.from_tree(node)
  File "\openpyxl\styles\stylesheet.py", line 102, in from_tree
    return super(Stylesheet, cls).from_tree(node)
  File "\openpyxl\descriptors\serialisable.py", line 103, in from_tree
    return cls(**attrib)
  File "\openpyxl\styles\stylesheet.py", line 73, in __init__
    self.fills = fills
  File "\openpyxl\descriptors\sequence.py", line 26, in __set__
    seq = [_convert(self.expected_type, value) for value in seq]
  File "\openpyxl\descriptors\sequence.py", line 26, in <listcomp>
    seq = [_convert(self.expected_type, value) for value in seq]
  File "\openpyxl\descriptors\base.py", line 57, in _convert
    raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'openpyxl.styles.fills.Fill'>

我可以做些什么来获得这个问题并像这里一样阅读 bg 颜色:Get cell color from .xlsx

【问题讨论】:

  • 您的 XLSX 文件中可能存在错误。

标签: python excel openpyxl


【解决方案1】:

所以我今天正在解决这个几乎相同的问题。我发现了一个非常整洁但有点奇怪的库,它似乎忽略了这个填充问题。 我生成了这些 .xlsx 文件,但它们无法使用 pandas/openpyxl 正确打开,出现相同的“填充不带参数”错误。

我在 pip 中找到了一个 xlsreader 模块: https://pypi.org/project/xlsxreader/

以下完美打开有问题的 .xlsx 文件,并将它们发送到 .csv 格式的临时文件中。

据我所知,这是它唯一能做的事情。我找不到太多的文档,但我已经收集了很多,它适用于这些文件,并且上个月刚刚更新。

import xlsreader
import csv

latest_xlsx_file = "D:\\testfile.xlsx"
# read it
thing = xlsxreader.readxlsx(latest_xlsx_file)
# get the name of the temporary .csv that was made
file = thing.name
# print the filename
print(file)
# open, or copy, or whatever the csv
with open(file, 'r') as CSV:
    read_csv = csv.reader(CSV, delimiter=",")
    read_csv = list(read_csv)
for r in read_csv:
    print(r)

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,后来解决了。你需要尝试pandas.read_excel(x, 'sheet1', index_col=None, engine='openpyxl'),特别是不要忘记“engine ='openpyxl'”。 熊猫保佑你!!!

    【讨论】:

    • 这没有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多