Python 读写 Excel
2016-11-22 21:34
211 查看
http://www.cnitblog.com/yunshichen/archive/2010/03/16/64661.html
基本上, 这个网页已经说明一切了: http://pypi.python.org/pypi/xlrd
等有时间再把这个页面写漂亮,现在先记一些代码.
from xlrd import open_workbook
wb = open_workbook('simple.xls','rb')
for s in wb.sheets():
print 'Sheet:',s.name
for row in range(s.nrows):
values=[]
for col in range(s.ncols):
values.append(s.cell(row,col).value)
print ",".join(values)
print
from xlwt import Workbook
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
book.add_sheet('Sheet 2')
sheet1.write(0,0,'A1')
sheet1.write(0,1,'B1')
row1 = sheet1.row(1)
row1.write(0,'A2')
row1.write(1,'B2')
sheet1.col(0).width = 10000
sheet2 = book.get_sheet(1)
sheet2.row(0).write(0,'Sheet 2 A1')
sheet2.row(0).write(1,'Sheet 2 B1')
sheet2.flush_row_data()
sheet2.write(1,0,'Sheet 2 A3')
sheet2.col(0).width = 5000
sheet2.col(0).hidden = True
book.save('simple2.xls')
book.save(TemporaryFile())
基本上, 这个网页已经说明一切了: http://pypi.python.org/pypi/xlrd
等有时间再把这个页面写漂亮,现在先记一些代码.
读Excel
先建个simple.xlsfrom xlrd import open_workbook
wb = open_workbook('simple.xls','rb')
for s in wb.sheets():
print 'Sheet:',s.name
for row in range(s.nrows):
values=[]
for col in range(s.ncols):
values.append(s.cell(row,col).value)
print ",".join(values)
写Excel
from tempfile import TemporaryFilefrom xlwt import Workbook
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
book.add_sheet('Sheet 2')
sheet1.write(0,0,'A1')
sheet1.write(0,1,'B1')
row1 = sheet1.row(1)
row1.write(0,'A2')
row1.write(1,'B2')
sheet1.col(0).width = 10000
sheet2 = book.get_sheet(1)
sheet2.row(0).write(0,'Sheet 2 A1')
sheet2.row(0).write(1,'Sheet 2 B1')
sheet2.flush_row_data()
sheet2.write(1,0,'Sheet 2 A3')
sheet2.col(0).width = 5000
sheet2.col(0).hidden = True
book.save('simple2.xls')
book.save(TemporaryFile())
相关文章推荐
- python操作Excel读写(使用xlrd和xlrt)
- python操作Excel读写(使用xlrd和xlrt)
- Python之Excel读写
- python 读写excel
- python类库26[读写Excel]
- python 读写excel
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python中对Excel进行读写操作
- Python读写Excel
- python操作Excel读写--使用xlrd
- python操作Excel读写(使用xlrd和xlrt)
- python26高级[读写Excel]
- python读写excel
- python操作Excel读写--使用xlrd
- python类库26[读写Excel]
- python 读写 Excel