Python操作Excel 

根据网上资料学习了一下Python对Excel的操作,有些问题,从网上搜索相关资料后,初步能够操作excel了。

写入Excel:

 

import xlwt
workbook = xlwt.Workbook(encoding='utf-8')
booksheet = workbook.add_sheet('Sheet 1', cell_overwrite_ok=True)
workbook.add_sheet('Sheet 2')
DATA = (('xh', 'xm', 'nl', 'xb', 'cj'),
 (1001, 'AAAA', 23, 'M', 98),
 (1002, 'BBBB', 21, 'F', 90),
 (1003, 'CCCC', 24, 'F', 100),
 (1004, 'DDDD', 22, 'F', 86),
 (1005, 'EEEE', 25, 'F', 88),)

for i, row in enumerate(DATA):
 for j, col in enumerate(row):
 booksheet.write(i, j, col)
booksheet.col(0).width=10
workbook.save('cjd.xls')

读取Excel:

import xlrd
wb = xlrd.open_workbook('cjd.xls')
ws = wb.sheets()[0]
dataset = []
for r in range(ws.nrows):
 col = []
 for c in range(ws.ncols):
 col.append(ws.cell(r,c).value)
 dataset.append(col)
from pprint import pprint
pprint(dataset)

下一步主要要解决一下汉字问题。

后来发现问题和php一样还是出现在源文件上,转化为utf-8格式就可以解决了。

来源: 使用Python代码处理Excel – ZhangYuLiang的个人空间 – 开源中国社区

Leave a Reply

您的电子邮箱地址不会被公开。 必填项已用 * 标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>