![](/img/trans.png)
[英]I am using the openpyxl library on python on a windows 10 computer and trying to load_workbook with whitespace
[英]Why I am not able to load excel files generated in the morning, but can load them in the afternoon in Python using Openpyxl
我正在使用Python Openpyxl導入由在線工具生成的excel文件。 導入早晨生成的文件時,出現如下錯誤:
Traceback (most recent call last):
File "test4.py", line 8, in <module>
wb = openpyxl.load_workbook (temp2)
File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 201, in load_workbook
wb.properties = DocumentProperties.from_tree(src)
File "C:\Python27\lib\site-packages\openpyxl\descriptors\serialisable.py", line 89, in from_tree
return cls(**attrib)
File "C:\Python27\lib\site-packages\openpyxl\packaging\core.py", line 106, in__init__
self.modified = modified
File "C:\Python27\lib\site-packages\openpyxl\descriptors\base.py", line 267, in __set__
value = W3CDTF_to_datetime(value)
File "C:\Python27\lib\site-packages\openpyxl\utils\datetime.py", line 40, in W3CDTF_to_datetime
dt = [int(v) for v in match.groups()[:6]]
AttributeError: 'NoneType' object has no attribute 'groups'
奇怪的是,當我導入由在線工具在早上生成的文件時,只會出現此錯誤。 我嘗試了相同的文件,但在下午生成了,效果很好。 我對問題出在哪里感到困惑。 Excel文件中沒有與時間相關的字段。 除修改時間外,早上和下午生成的文件完全相同。 有人可以幫我嗎? 謝謝。
通過此在線工具創建的Excel文件與openpyxl不太兼容
通過通過zipfile
打開excel文件,函數load_workbook
將獲取工作簿級別的信息,並從'docProps / core.xml'將其分配給Workbook()的wb.properties
。 一條信息就是修改時間。
Modifyed的值會引發錯誤,無法將其傳輸到datetime
。 “修改”的模式必須為openpyxl.utils.datetime.W3CDTF_REGEX
,即W3CDTF | W3C日期和時間格式
您可以檢查excel的修改時間是否與W3CDTF相對應。 這是代碼:
from openpyxl.reader.excel import _validate_archive
archive = _validate_archive('/path/to/yourexcel.xlsx')
valid_files = archive.namelist()
# you'll find 'xx/core.xml' I'm not sure if it's 'docProps/core.xml'
print valid_files
# read 'xx/core.xml'
wb_info = archive.read('docProps/core.xml')
print wb_info
在wb_info中 ,您會發現類似
<dcterms:modified xsi:type="dcterms:W3CDTF">2017-04-01T22:48:48Z</dcterms:modified>
。
從在線工具和您的PC對比Excel文件的wb_info 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.