首页 > 解决方案 > python openpyxl.load_workbook TypeError: Fill() 不带参数

问题描述

我想从 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

标签: pythonexcelopenpyxl

解决方案


所以我今天正在解决这个几乎相同的问题。我发现了一个非常整洁但有点奇怪的库,它似乎忽略了这个填充问题。我正在生成这些 .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)

推荐阅读