python计算excel表格有字符_python xlutils 怎么实现 Excel的字符转换为指定字符

展开全部

# -*- coding:utf-8 -*-

from xlrd import open_workbook

from xlutils.copy import copy

def getrule(rfile='test.txt'):

try:

rdict={}

with open(rfile,'r') as f:

for line in f:

rline=line.split('->')

rdict[rline[0].strip()]=rline[1].strip()

return rdict

except Exception,e:

print e

if __name__ == '__main__':

excelfile='test.xls'

rdict=getrule()

rb = open_workbook(excelfile)

rs = rb.sheet_by_index(0)

wb = copy(rb)

ws = wb.get_sheet(0)

nrows = rs.nrows

ncols = rs.ncols

for i in range(nrows):

for j in range(ncols):

cvalue=rs.cell(i,j).value

if type(cvalue).__name__=='float':

cvalue=str(int(cvalue))

if rdict.has_key(cvalue):

print '%s is replaced by %s' %(cvalue,rdict[cvalue])

ws.write(i, j, rdict[cvalue])

wb.save(excelfile)

其中test.xls存放需替换的xls文件,62616964757a686964616fe78988e69d8331333335333639test.txt存放规则文件,代码如上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值