openpyxl+load_workbook+AttributeError: 'NoneType' object has no attribute 'date1904'
openpyxl+load_workbook+AttributeError: 'NoneType' object has no attribute 'date1904'
当我使用 openpyxl 加载 Excel 文件(.xlsx)时,显示此错误(最后 link 是示例 Excel 文件):
from openpyxl import *
wb = load_workbook("D:/develop/workspace/exman/test sample/510001653.xlsx")
Traceback (most recent call last):
File "", line 1, in
File "C:\Python34\lib\site-packages\openpyxl-2.5.0-py3.4.egg\openpyxl\reader\
xcel.py", line 161, in load_workbook
parser.parse()
File "C:\Python34\lib\site-packages\openpyxl-2.5.0-py3.4.egg\openpyxl\packagi
g\workbook.py", line 42, in parse
if package.properties.date1904:
AttributeError: 'NoneType' object has no attribute 'date1904'
我调试 python 文件,发现 workbookPr = None ,导致 package.properties 到 None( properties = Alias(workbookPr)。所以我像下面这样更改 workbookParser.parser() 的代码,错误已解决。
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
........
此错误已在较新版本中修复(我检查了 2.4.8 并已修复。2.4.0 仍然存在)
pip install --upgrade openpyxl
当我使用 openpyxl 加载 Excel 文件(.xlsx)时,显示此错误(最后 link 是示例 Excel 文件):
from openpyxl import * wb = load_workbook("D:/develop/workspace/exman/test sample/510001653.xlsx")
Traceback (most recent call last):
File "", line 1, in
File "C:\Python34\lib\site-packages\openpyxl-2.5.0-py3.4.egg\openpyxl\reader\ xcel.py", line 161, in load_workbook
parser.parse()
File "C:\Python34\lib\site-packages\openpyxl-2.5.0-py3.4.egg\openpyxl\packagi g\workbook.py", line 42, in parse
if package.properties.date1904:
AttributeError: 'NoneType' object has no attribute 'date1904'
我调试 python 文件,发现 workbookPr = None ,导致 package.properties 到 None( properties = Alias(workbookPr)。所以我像下面这样更改 workbookParser.parser() 的代码,错误已解决。
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
........
此错误已在较新版本中修复(我检查了 2.4.8 并已修复。2.4.0 仍然存在)
pip install --upgrade openpyxl