[英]Why I am not able to load excel files generated in the morning, but can load them in the afternoon in Python using Openpyxl
I am using Python Openpyxl to import excel files which are generated by a online tool. 我正在使用Python Openpyxl导入由在线工具生成的excel文件。 When I import the files generated in the morning, I got an error like this:
导入早晨生成的文件时,出现如下错误:
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'
The strange thing is I only got this error when I importing the files which are generated by the online tool in the morning. 奇怪的是,当我导入由在线工具在早上生成的文件时,只会出现此错误。 I tried the same file but generated in the afternoon, it works very well.
我尝试了相同的文件,但在下午生成了,效果很好。 I'm confused where the problem is.
我对问题出在哪里感到困惑。 There are no fields in the excel files related to time.
Excel文件中没有与时间相关的字段。 And the files generated in the morning and in the afternoon are exactly the same except the modified time.
除修改时间外,早上和下午生成的文件完全相同。 Does anybody can help me with it?
有人可以帮我吗? Thank you.
谢谢。
Excel files created from this online tool isn't well compatible with openpyxl 通过此在线工具创建的Excel文件与openpyxl不太兼容
The function load_workbook
will get workbook-level information and assign to Workbook()'s wb.properties
from 'docProps/core.xml' by opening excel file through zipfile
. 通过通过
zipfile
打开excel文件,函数load_workbook
将获取工作簿级别的信息,并从'docProps / core.xml'将其分配给Workbook()的wb.properties
。 One piece of information is modified time. 一条信息就是修改时间。
The value of modified raise the error, it can't be transported into datetime
. Modifyed的值会引发错误,无法将其传输到
datetime
。 The pattern of 'modified' must be openpyxl.utils.datetime.W3CDTF_REGEX
, which is W3CDTF|W3C Date and Time Formats “修改”的模式必须为
openpyxl.utils.datetime.W3CDTF_REGEX
,即W3CDTF | W3C日期和时间格式
You can check the excel's modified time if it corresponds to W3CDTF. 您可以检查excel的修改时间是否与W3CDTF相对应。 Here is the code:
这是代码:
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
In wb_info , you will find something like 在wb_info中 ,您会发现类似
<dcterms:modified xsi:type="dcterms:W3CDTF">2017-04-01T22:48:48Z</dcterms:modified>
. <dcterms:modified xsi:type="dcterms:W3CDTF">2017-04-01T22:48:48Z</dcterms:modified>
。
Contrast wb_info of excel files from online tool and your pc. 从在线工具和您的PC对比Excel文件的wb_info 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.