python - 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?
解决方案
所以我今天正在解决这个几乎相同的问题。我发现了一个非常整洁但有点奇怪的库,它似乎忽略了这个填充问题。我正在生成这些 .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)
推荐阅读
- python - 如何访问按键名存储键值对的 MongoDB 数组
- python - pandas df.fillna() 不替换 na 值
- odoo-13 - 将默认地址添加到帐户移动
- python - transform array of arrays into arguments for a function in Python
- listview - 从外部 fxml 按钮事件将项目添加到 listView
- database - Grafana 字段的计时器
- enterprise-architect - 这个符号在 Sparx Enterprise Architect 中是什么意思
- html - 如何在 django_ploty_dash 环境中实现 dash.callback_context 函数?
- java - 当应用程序被杀死时,AlertDialog 不会出现在屏幕上
- linux - 如何在不使用 Kubernetes (PKS) 中的 cron 作业的情况下运行节点自动缩放脚本