openpyxl + load_workbook + AttributeError:"NoneType"对象没有属性"date1904" [英] openpyxl+load_workbook+AttributeError: 'NoneType' object has no attribute 'date1904'
问题描述
当我使用openpyxl加载Excel文件(.xlsx)时,会显示此错误(最后一个链接是示例Excel文件):
When I use openpyxl to load the Excel file( .xlsx), this error displays (the last the link is the sample Excel file):
从openpyxl导入* wb = load_workbook("D:/develop/workspace/exman/test sample/510001653.xlsx")
from openpyxl import * wb = load_workbook("D:/develop/workspace/exman/test sample/510001653.xlsx")
回溯(最近通话最近一次):
Traceback (most recent call last):
文件",位于
文件"C:\ Python34 \ lib \ site-packages \ openpyxl-2.5.0-py3.4.egg \ openpyxl \ reader \ xcel.py",第161行,在load_workbook中
File "C:\Python34\lib\site-packages\openpyxl-2.5.0-py3.4.egg\openpyxl\reader\ xcel.py", line 161, in load_workbook
parser.parse()
parser.parse()
文件"C:\ Python34 \ lib \ site-packages \ openpyxl-2.5.0-py3.4.egg \ openpyxl \ packagi g \ workbook.py",第42行,处于解析状态
File "C:\Python34\lib\site-packages\openpyxl-2.5.0-py3.4.egg\openpyxl\packagi g\workbook.py", line 42, in parse
如果package.properties.date1904:
if package.properties.date1904:
AttributeError:'NoneType'对象没有属性'date1904'
推荐答案
我调试了python文件,并发现workbookPr = None,导致package.properties变为None(properties = Alias(workbookPr).如下的workbookParser.parser()代码,该错误已解决.
I debug the python file ,and find that the workbookPr = None , cause the package.properties to None( properties = Alias(workbookPr). So I change the code of workbookParser.parser() like follow, the error is solved.
class WorkbookParser:
def __init__(self, archive):
self.archive = archive
self.wb = Workbook()
self.sheets = []
self.rels = get_dependents(self.archive, ARC_WORKBOOK_RELS)
def parse(self):
src = self.archive.read(ARC_WORKBOOK)
node = fromstring(src)
package = WorkbookPackage.from_tree(node)
if package.properties is not None: #add this line
if package.properties.date1904:
wb.excel_base_date = CALENDAR_MAC_1904
self.wb.code_name = package.properties.codeName
self.wb.active = package.active
..........
..........
这篇关于openpyxl + load_workbook + AttributeError:"NoneType"对象没有属性"date1904"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!