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):
文件",第1行,在
文件C:Python34libsite-packagesopenpyxl-2.5.0-py3.4.eggopenpyxl eaderxcel.py",第 161 行,在 load_workbook 中
File "C:Python34libsite-packagesopenpyxl-2.5.0-py3.4.eggopenpyxl eader xcel.py", line 161, in load_workbook
parser.parse()
parser.parse()
文件C:Python34libsite-packagesopenpyxl-2.5.0-py3.4.eggopenpyxlpackagigworkbook.py",第 42 行,解析中
File "C:Python34libsite-packagesopenpyxl-2.5.0-py3.4.eggopenpyxlpackagi gworkbook.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屋!