简体   繁体   English

为什么我不能加载早上生成的Excel文件,但可以使用Openpyxl在Python中下午加载它们

[英]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.

相关问题 我正在Windows 10计算机上的python上使用openpyxl库,并尝试使用空白load_workbook - I am using the openpyxl library on python on a windows 10 computer and trying to load_workbook with whitespace 无法在python中使用openpyxl写入excel - Not able to write to excel using openpyxl in python 我正在使用 openpyxl 在模板中加载数据,当我打开时我在目标文件中收到警告 - I am using openpyxl to load data in template and i am getting warning in the destination file when i open 我无法在 Python 中使用 Openpyxl 加载我的工作簿 - I cannot load my workbook using Openpyxl in Python 为什么我不能使用python和openpyxl在excel中更改单元格值? - Why can't I change the cell value in excel using python and openpyxl? 我无法使用 CGI-Python 脚本加载 AWS 凭证 - I am not able to load AWS credentials using CGI-Python script 无法读取excel文件,使用openpyxl - Can't read excel files, using openpyxl 将应用程序与 pyinstaller 捆绑后,无法使用 openpyxl 在 Excel 中保留或/和加载图像 - Can't keep or/and load image in Excel using openpyxl after bundling the application with pyinstaller 在 Python 数据框中获取一天的一部分(早上、下午、晚上、晚上) - Get part of day (morning, afternoon, evening, night) in Python dataframe Python openpyxl 库拒绝加载我的 excel 文档 - Python openpyxl library refusing to load my excel document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM