使用extract_msg提取.msg文件日期时出现Message对象无_prop属性及cielDiv未定义错误如何解决
问题描述
- 业务目标:在Python环境中通过
extract_msg包读取.msg格式邮件文件,提取邮件日期字段。 - 环境差异:当前报错设备Python版本为3.8.5,可正常运行相同代码的设备Python版本为3.8.0,两台设备使用完全相同的.msg测试文件,已排除文件损坏导致异常的可能。
- 初始测试代码:
import extract_msg f = r'.msg 文件实际路径' msg = extract_msg.Message(f)
完整报错栈
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\extract_msg\msg.py in mainProperties(self) 409 try: --> 410 return self._prop 411 except AttributeError: AttributeError: 'Message' object has no attribute '_prop' During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) <ipython-input-115-2c4a84bf5176> in <module> 2 3 f = r'.msg file' # 替换为实际文件路径 ----> 4 msg = extract_msg.Message(f) 5 # msg_sender = msg.sender 6 # msg_date = msg.date ~\Anaconda3\lib\site-packages\extract_msg\message.py in __init__(self, path, prefix, attachmentClass, filename, delayAttachments, overrideEncoding) 26 27 def __init__(self, path, prefix = '', attachmentClass = Attachment, filename = None, delayAttachments = False, overrideEncoding = None): ---> 28 MessageBase.__init__(self, path, prefix, attachmentClass, filename, delayAttachments, overrideEncoding) 29 30 def dump(self): ~\Anaconda3\lib\site-packages\extract_msg\message_base.py in __init__(self, path, prefix, attachmentClass, filename, delayAttachments, overrideEncoding) 48 # TODO have each function check for initialization of needed data so these 49 # lines will be unnecessary. ---> 50 self.mainProperties 51 self.header 52 self.recipients ~\Anaconda3\lib\site-packages\extract_msg\msg.py in mainProperties(self) 410 return self._prop 411 except AttributeError: ---> 412 self._prop = Properties(self._getStream('__properties_version1.0'), 413 constants.TYPE_MESSAGE if self.prefix == '' else constants.TYPE_MESSAGE_EMBED) 414 return self._prop ~\Anaconda3\lib\site-packages\extract_msg\properties.py in __init__(self, stream, type, skip) 50 if skip == 0: 51 skip = 32 ---> 52 streams = divide(self.__stream[skip:], 16) 53 for st in streams: 54 a = create_prop(st) ~\Anaconda3\lib\site-packages\extract_msg\utils.py in divide(string, length) 141 ['Hello', ' Worl', 'd!'] 142 """ --> 143 return [string[length * x:length * (x + 1)] for x in range(int(cielDiv(len(string), length)))] 144 145 def fromTimeStamp(stamp): NameError: name 'cielDiv' is not defined
根因定位
- 报错链路中的
AttributeError: 'Message' object has no attribute '_prop'是初始化流程中断后的次生异常,并非问题根源。 - 实际触发点为
extract_msg包内部utils.py模块的divide函数,调用了当前版本中未定义、未导入的cielDiv函数,触发NameError,直接导致Message对象的初始化流程中断,连带出现了_prop属性缺失的报错。 - 问题和业务代码逻辑、.msg文件本身无关,本质是
extract_msg包的版本兼容问题:不同Python版本环境下安装的包源码版本不一致,存在函数名拼写迭代、导出逻辑缺失的断层。
注意:cielDiv是旧版本包中的拼写,新版本已修正为ceilDiv,直接替换拼写即可解决函数未定义的问题
解决方案
- 方案1:对齐可正常运行环境的包版本
- 在可正常运行的设备上执行命令查询当前可用的
extract_msg版本:pip show extract_msg - 在当前报错设备上执行指定版本安装命令,替换不一致的包版本:
pip install extract_msg==[查询到的正常版本号]
- 在可正常运行的设备上执行命令查询当前可用的
- 方案2:修复本地包源码的缺失问题
- 定位到报错栈中提示的本地路径
~\Anaconda3\lib\site-packages\extract_msg\utils.py文件 - 找到
divide函数中调用cielDiv的代码行,将原有错误拼写的cielDiv替换为修正后的ceilDiv,保存文件即可。
- 定位到报错栈中提示的本地路径
- 修复验证:运行简化测试代码,可正常输出邮件日期值即为修复完成
import extract_msg msg = extract_msg.Message(r'你的.msg文件实际路径') print(msg.date)
内容的提问来源于stack exchange,提问作者Zeynabr
相关产品推荐
相关产品推荐

