python2 使用openpyxl 报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in positio

Posted 小~yytt~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python2 使用openpyxl 报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in positio相关的知识,希望对你有一定的参考价值。

# -*- coding: utf-8 -*-
#! /usr/bin/env python
# author: xiao~~

import openpyxl
import sys
reload(sys)
sys.setdefaultencoding(\'utf-8\')

wb2=openpyxl.Workbook()

wb2.save(\'test1.xlsx\')
print(\'新建成功\')

  附上剪短的代码

报错信息

Traceback (most recent call last):
File "F:\\PyCharm 5.0.4\\helpers\\pydev\\pydevd.py", line 2411, in <module>
globals = debugger.run(setup[\'file\'], None, None, is_module)
File "F:\\PyCharm 5.0.4\\helpers\\pydev\\pydevd.py", line 1802, in run
launch(file, globals, locals) # execute the script
File "D:/test/yf_test/conf/test1.py", line 5, in <module>
import openpyxl
File "F:\\Python27\\lib\\site-packages\\openpyxl\\__init__.py", line 29, in <module>
from openpyxl.workbook import Workbook
File "F:\\Python27\\lib\\site-packages\\openpyxl\\workbook\\__init__.py", line 5, in <module>
from .workbook import Workbook
File "F:\\Python27\\lib\\site-packages\\openpyxl\\workbook\\workbook.py", line 17, in <module>
from openpyxl.writer.excel import save_workbook, save_dump
File "F:\\Python27\\lib\\site-packages\\openpyxl\\writer\\excel.py", line 31, in <module>
from openpyxl.packaging.manifest import Manifest
File "F:\\Python27\\lib\\site-packages\\openpyxl\\packaging\\manifest.py", line 41, in <module>
mimetypes.init()
File "F:\\Python27\\lib\\mimetypes.py", line 358, in init
db.read_windows_registry()
File "F:\\Python27\\lib\\mimetypes.py", line 258, in read_windows_registry
for subkeyname in enum_types(hkcr):
File "F:\\Python27\\lib\\mimetypes.py", line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: \'ascii\' codec can\'t decode byte 0xd7 in position 20: ordinal not in range(128)

 

将F:\\Python27\\Lib\\mimetypes.py这个文档下屏蔽就可以

 

我已验证可以成功解决

 

以上是关于python2 使用openpyxl 报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in positio的主要内容,如果未能解决你的问题,请参考以下文章

Openpyxl 保存损坏/不可读的文件。没有报错,只是shell重启

有没有办法使用 openpyxl 读取没有工作簿的 Excel 文件?

在python 2.7中安装openpyxl

安装python -m pip install openpyxl 类库报错,A new release of pip available

使用 Python 2.7 和 openpyxl 删除 Excel 中的单元格

安装第三方库openpyxl时,报错:You are using pip version 19.0.3, however version 20.2b1 is available. You should