其实python对中文的支持很好,主要是源于python对unicode的支持
先说案例,我现在需要把一个目录下的文件名字写入到另外一个excel的sheet1的C列中,同时,文件名字开头的数字要和excel的sheet1中A列的值一致
即:
需要的插件:xlrd,xlwt,xlutils
下面是python代码:
#coding=utf-8
import os
import sys
from xlrd import open_workbook
from xlutils.copy import copy
filename = u"E:\\test\\excel\\北京市.xls"
rb = open_workbook(filename)
table = rb.sheets()[0]
workbook = copy(rb)
sheet = workbook.get_sheet(0)
homepath = u"E:\\test\\words"
wordnamelist = [os.path.splitext(f)[0] for f in os.listdir(homepath) if os.path.isfile(os.path.join(homepath, f))]
for row_index in range(table.nrows):
n = table.cell(row_index,0).value
for f in wordnamelist:
num,name = f.split()
if int(num)==n:
sheet.write(row_index,2,f)
wordnamelist.remove(f)
break
workbook.save(filename)
print "OK!"
在第6行和11行要显示的指明使用的是unicode编码(字符串加上u)
转载于:https://blog.51cto.com/herculeser/1255479