python写excel文件出错_【求教】xlutils修改中文Excel文件出错

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#coding=utf-8

import xlrd

import xlwt

from xlutils.copy import copy

sheetIndex=-1

file=xlrd.open_workbook(u'中文文件.xlsx')

writeFile=copy(file)

writeFile.encoding='utf8'

for sheet in file.sheets():

sheetIndex=sheetIndex+1

for rownum in range(1,sheet.nrows):

key=sheet.cell(rownum,4).value

val=sheet.cell(rownum,3).value

writeSheet=writeFile.get_sheet(sheetIndex)

writeSheet.write(rownum,4,(key+u'啊').encode('utf8'))

writeSheet.write(rownum,3,(val+u'1').encode('utf8')

writeFile.save(u'中文文件.xlsx')

下面是出错信息

Traceback (most recent call last):

File "D:\PythonTest\ReplaceNationName.py", line 28, in

writeFile.save(u'中文文件.xlsx')

File "D:\Program Files\Python276\lib\site-packages\xlwt\Workbook.py", line 662, in save

doc.save(filename, self.get_biff_data())

File "D:\Program Files\Python276\lib\site-packages\xlwt\Workbook.py", line 637, in get_biff_data

shared_str_table = self.__sst_rec()

File "D:\Program Files\Python276\lib\site-packages\xlwt\Workbook.py", line 599, in __sst_rec

return self.__sst.get_biff_record()

File "D:\Program Files\Python276\lib\site-packages\xlwt\BIFFRecords.py", line 76, in get_biff_record

self._add_to_sst(s)

File "D:\Program Files\Python276\lib\site-packages\xlwt\BIFFRecords.py", line 91, in _add_to_sst

u_str = upack2(s, self.encoding)

File "D:\Program Files\Python276\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2

us = unicode(s, encoding)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值