【发布时间】:2020-06-14 19:52:26
【问题描述】:
我有一个 .xlsx 文件,其中包含 2 个工作表。第一个包含常规数据(没什么花哨的),而第二个包含数据透视表。我只需要第一个工作表中的数据,而我想忽略第二个工作表,但是当调用 openpyxl.load_workbook 时,数据透视表会引发错误:TypeError: expected <type 'basestring'>。
错误出现在:openpyxl/reader/excel.py,在行:pivot_caches = parser.pivot_caches。
我尝试使用openpyxl 版本2.6.4 和2.5.1。我正在使用 Python 2.7。
删除第二张工作表后,错误消失了,并且正确读取了第一张工作表中的数据。但是,这些文件是由用户上传的,虽然我不需要数据透视表,但如果可能的话,我想避免强迫用户删除不必要的工作表。
示例代码:
from io import BytesIO
import openpyxl
pivot = '~/Downloads/file_with_pivot_tables.xlsx'
with open(pivot) as fin:
content = BytesIO(fin.read())
wb = openpyxl.load_workbook(content) # this line fails
ws = wb.get_sheet_by_name('Sheet1')
整个错误跟踪:
File "/Users/gi/lib/openpyxl/reader/excel.py", line 224, in load_workbook
pivot_caches = parser.pivot_caches
File "/Users/gi/lib/openpyxl/packaging/workbook.py", line 125, in pivot_caches
cache = get_rel(self.archive, self.rels, id=c.id, cls=CacheDefinition)
File "/Users/gi/lib/openpyxl/packaging/relationship.py", line 162, in get_rel
obj.deps = get_dependents(archive, rels_path)
File "/Users/gi/lib/openpyxl/packaging/relationship.py", line 130, in get_dependents
rels = RelationshipList.from_tree(node)
File "/Users/gi/lib/openpyxl/descriptors/serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
File "/Users/gi/lib/openpyxl/descriptors/serialisable.py", line 100, in from_tree
return cls(**attrib)
File "/Users/gi/lib/openpyxl/packaging/relationship.py", line 50, in __init__
self.Target = Target
File "/Users/gi/lib/openpyxl/descriptors/base.py", line 44, in __set__
raise TypeError('expected ' + str(self.expected_type))
TypeError: expected <type 'basestring'>
【问题讨论】:
-
我从未使用过 BytesIO,所以我不确定你想用它做什么。我认为
content不是load_workbook的可读文件路径,所以这可能会产生错误。为什么不直接引用文件路径呢? -
请分享整个错误信息。你为什么用
BytesIO? -
@AMC 我添加了错误堆栈跟踪。我使用 BytesIO 是因为我无法直接从文件中读取,因为我工作的环境是 GCP。
-
@giliev 哦,原来代码不是这样的?
-
是的,但错误是一样的,即它可以用我在这里提供的代码重现
标签: python excel python-2.7 openpyxl xlsx