python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及 python 对 excel 的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的 "研究"(用此一词让我觉得颇有成就感)之后,基本解决了日常所需。主要记录使用过程的常见问题及解决。
python 操作 excel 主要用到 xlrd 和 xlwt 这两个库,即 xlrd 是读 excel,xlwt 是写 excel 的库。可从这里下载https://pypi.python.org/pypi。下面分别记录 python 读和写 excel.
python 读 excel——xlrd
这个过程有几个比较麻烦的问题,比如读取日期、读合并单元格内容。下面先看看基本的操作:
首先读一个 excel 文件,有两个 sheet,测试用第二个 sheet,sheet2 内容如下:
python 对 excel 基本的操作如下:
# -*- coding: utf-8 -*-
import xlrd
import xlwt
from datetime import date,datetime
def read_excel():
# 打开文件
workbook = xlrd.open_workbook(r'F:\demo.xlsx')
# 获取所有 sheet
print workbook.sheet_names() # [u'sheet1', u'sheet2']
sheet2_name = workbook.sheet_names()[1]
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 根据sheet索引或者名称获取sheet内容</span>
sheet2 = workbook.sheet_by_index(1) <span style="color: rgba(0, 128, 0, 1)"># </span><span style="color: rgba(0, 128, 0, 1)">sheet索引从0开始</span>
sheet2 = workbook.sheet_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sheet2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sheet的名称,行数,列数</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> sheet2.name,sheet2.nrows,sheet2.ncols
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取整行和整列的值(数组)</span>
rows = sheet2.row_values(3) <span style="color: rgba(0, 128, 0, 1)"># </span><span style="color: rgba(0, 128, 0, 1)">获取第四行内容</span>
cols = sheet2.col_values(2) <span style="color: rgba(0, 128, 0, 1)"># </span><span style="color: rgba(0, 128, 0, 1)">获取第三列内容</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> rows
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> cols
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取单元格内容</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.cell(1,0).value.encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.cell_value(1,0).encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.row(1)[0].value.encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取单元格内容的数据类型</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> <strong>sheet2.cell(1<span style="color: rgba(0, 0, 0, 1)">,0).ctype
if name == 'main':
read_excel()
运行结果如下:
那么问题来了,上面的运行结果中红框框中的字段明明是出生日期,可显示的确实浮点数。好的,来解决第一个问题:
1、python 读取 excel 中单元格内容为日期的方式
python 读取 excel 中单元格的内容返回的有 5 种类型,即上面例子中的 ctype:
ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
即 date 的 ctype=3,这时需要使用 xlrd 的 xldate_as_tuple 来处理为 date 格式,先判断表格的 ctype=3 时 xldate 才能开始操作。现在命令行看下:
>>> sheet2.cell(2,2).ctype #1990/2/22 3 >>> sheet2.cell(2,1).ctype #24 2 >>> sheet2.cell(2,0).ctype #小胖 1 >>> sheet2.cell(2,4).ctype #空值(这里是合并单元格的原因) 0 >>> sheet2.cell(2,2).value #1990/2/22 33656.0 >>> xlrd.xldate_as_tuple(sheet2.cell_value(2,2),workbook.datemode) (1992, 2, 22, 0, 0, 0) >>> date_value = xlrd.xldate_as_tuple(sheet2.cell_value(2,2),workbook.datemode) >>> date_value (1992, 2, 22, 0, 0, 0)
>>> date(*date_value[:3])
datetime.date(1992, 2, 22)
>>> date(*date_value[:3]).strftime('%Y/%m/%d')
'1992/02/22'
即可以做下简单处理,判断 ctype 是否等于 3,如果等于 3,则用时间格式处理:
if (sheet.cell(row,col).ctype == 3):
date_value = xlrd.xldate_as_tuple(sheet.cell_value(rows,3),book.datemode)
date_tmp = date(*date_value[:3]).strftime('%Y/%m/%d')
那么问题又来了,上面 sheet2.cell(2,4).ctype 返回的值是 0,说明这个单元格的值是空值,明明是合并的单元格内容 "好朋友",这个是我觉得这个包功能不完善的地方,如果是合并的单元格那么应该合并的单元格的内容一样,但是它只是合并的第一个单元格的有值,其它的为空。
>>> sheet2.col_values(4)
[u'\u5173\u7cfb', u'\u597d\u670b\u53cb', '', u'\u540c\u5b66', '', '', u'\u4e00\u4e2a\u4eba', '']
>>> for i in range(sheet2.nrows):
print sheet2.col_values(4)[i]
关系
好朋友
同学
一个人
>>> sheet2.row_values(7)
[u'\u65e0\u540d', 20.0, u'\u6682\u65e0', '', '']
>>> for i in range(sheet2.ncols):
print sheet2.row_values(7)[i]
无名
20.0
暂无
>>>
2、读取合并单元格的内容
这个是真没技巧,只能获取合并单元格的第一个 cell 的行列索引,才能读到值,读错了就是空值。
即合并行单元格读取行的第一个索引,合并列单元格读取列的第一个索引,如上述,读取行合并单元格 "好朋友" 和读取列合并单元格 "暂无" 只能如下方式:
>>> print sheet2.col_values(4)[1]
好朋友
>>> print sheet2.row_values(7)[2]
暂无
>>> sheet2.merged_cells # 明明有合并的单元格,为何这里是空
[]
疑问又来了,合并单元格可能出现空值,但是表格本身的普通单元格也可能是空值,要怎么获取单元格所谓的 "第一个行或列的索引" 呢?
这就要先知道哪些是单元格是被合并的!
3、获取合并的单元格
读取文件的时候需要将 formatting_info 参数设置为 True,默认是 False,所以上面获取合并的单元格数组为空,
>>> workbook = xlrd.open_workbook(r'F:\demo.xlsx',formatting_info=True)
>>> sheet2 = workbook.sheet_by_name('sheet2')
>>> sheet2.merged_cells
[(7, 8, 2, 5), (1, 3, 4, 5), (3, 6, 4, 5)]
merged_cells 返回的这四个参数的含义是:(row,row_range,col,col_range), 其中 [row,row_range) 包括 row, 不包括 row_range,col 也是一样,即 (1, 3, 4, 5) 的含义是:第 1 到 2 行(不包括 3)合并,(7, 8, 2, 5)的含义是:第 2 到 4 列合并。
利用这个,可以分别获取合并的三个单元格的内容:
>>> print sheet2.cell_value(1,4) #(1, 3, 4, 5)
好朋友
>>> print sheet2.cell_value(3,4) #(3, 6, 4, 5)
同学
>>> print sheet2.cell_value(7,2) #(7, 8, 2, 5)
暂无
发现规律了没?是的,获取 merge_cells 返回的 row 和 col 低位的索引即可! 于是可以这样一劳永逸:
>>> merge = []
>>> for (rlow,rhigh,clow,chigh) in sheet2.merged_cells:
merge.append([rlow,clow])
>>> merge
[[7, 2], [1, 4], [3, 4]]
>>> for index in merge:
print sheet2.cell_value(index[0],index[1])
暂无
好朋友
同学
>>>
python 写 excel——xlwt
写 excel 的难点可能不在构造一个 workbook 的本身,而是填充的数据,不过这不在范围内。在写 excel 的操作中也有棘手的问题,比如写入合并的单元格就是比较麻烦的,另外写入还有不同的样式。这些要看源码才能研究的透。
我 "构思" 了如下面的 sheet1,即要用 xlwt 实现的东西:
基本上看起来还算复杂,而且看起来 "很正规",完全是个人杜撰。
代码如下:
'''
设置单元格样式
'''
def set_style(name,height,bold=False):
style = xlwt.XFStyle() # 初始化样式
font = xlwt.Font() # 为样式创建字体
font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders= xlwt.Borders()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.left= 6</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.right= 6</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.top= 6</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.bottom= 6</span>
style.font = font
# style.borders = borders
<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> style
#写 excel
def write_excel():
f = xlwt.Workbook() #创建工作簿
<span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
创建第一个sheet:
sheet1
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
sheet1 </span>= f.add_sheet(u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sheet1</span><span style="color: rgba(128, 0, 0, 1)">'</span>,cell_overwrite_ok=True) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">创建sheet</span>
row0 = [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">业务</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">状态</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">上海</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">广州</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">深圳</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">状态小计</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">合计</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
column0 </span>= [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">机票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">船票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">火车票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">汽车票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">其它</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
status </span>= [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">预订</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">出票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">退票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">业务小计</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一行</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(row0)):
sheet1.write(0,i,row0[i],set_style(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">,True))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一列和最后一列(合并4行)</span>
i, j = 1<span style="color: rgba(0, 0, 0, 1)">, 0
</span><span style="color: rgba(0, 0, 255, 1)">while</span> i < 4*len(column0) <span style="color: rgba(0, 0, 255, 1)">and</span> j <<span style="color: rgba(0, 0, 0, 1)"> len(column0):
sheet1.write_merge(i,i</span>+3,0,0,column0[j],set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Arial</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220,True)) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">第一列</span>
sheet1.write_merge(i,i+3,7,7) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">最后一列"合计"</span>
i += 4<span style="color: rgba(0, 0, 0, 1)">
j </span>+= 1<span style="color: rgba(0, 0, 0, 1)">
sheet1.write_merge(</span>21,21,0,1,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">合计</span><span style="color: rgba(128, 0, 0, 1)">'</span>,set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">,True))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第二列</span>
i =<span style="color: rgba(0, 0, 0, 1)"> 0
</span><span style="color: rgba(0, 0, 255, 1)">while</span> i < 4*<span style="color: rgba(0, 0, 0, 1)">len(column0):
</span><span style="color: rgba(0, 0, 255, 1)">for</span> j <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(status)):
sheet1.write(j</span>+i+1,1<span style="color: rgba(0, 0, 0, 1)">,status[j])
i </span>+= 4<span style="color: rgba(0, 0, 0, 1)">
f.save(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">demo1.xlsx</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">保存文件</span>
if name == 'main':
#generate_workbook()
#read_excel()
write_excel()
需要稍作解释的就是 write_merge 方法:
write_merge(x, x + m, y, w + n, string, sytle)
x 表示行,y 表示列,m 表示跨行个数,n 表示跨列个数,string 表示要写入的单元格内容,style 表示单元格样式。其中,x,y,w,h,都是以 0 开始计算的。
这个和 xlrd 中的读合并单元格的不太一样。
如上述:sheet1.write_merge(21,21,0,1,u'合计',set_style('Times New Roman',220,True))
即在 22 行合并第 1,2 列,合并后的单元格内容为 "合计",并设置了 style。
如果需要创建多个 sheet,则只要 f.add_sheet 即可。
如在上述 write_excel 函数里 f.save('demo1.xlsx') 这句之前再创建一个 sheet2,效果如下:
代码也是真真的 easy 的了:
'''
创建第二个 sheet:
sheet2
'''
sheet2 = f.add_sheet(u'sheet2',cell_overwrite_ok=True) #创建 sheet2
row0 = [u'姓名',u'年龄',u'出生日期',u'爱好',u'关系']
column0 = [u'小杰',u'小胖',u'小明',u'大神',u'大仙',u'小敏',u'无名']
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一行</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(row0)):
sheet2.write(0,i,row0[i],set_style(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">,True))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一列</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(column0)):
sheet2.write(i</span>+1,0,column0[i],set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">))
sheet2.write(</span>1,2,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1991/11/11</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sheet2.write_merge(</span>7,7,2,4,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">暂无</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">合并列单元格</span>
sheet2.write_merge(1,2,4,4,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">好朋友</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">合并行单元格</span>
f.save('demo1.xlsx') #保存文件
还可以添加超链接,如:
# 添加超链接 n= "HYPERLINK" sheet2.write_merge(9,9,2,8, xlwt.Formula(n +'("http://www.cnblogs.com/zhoujie";"jzhou\'s blog")'),set_style('Arial',300,True)) sheet2.write_merge(10,10, 2, 8, xlwt.Formula(n +'("mailto:zhoujie0111@126.com";"contact me")'),set_style('Arial',300,True))
完整代码:
# -*- coding: utf-8 -*- import xlwt import xlrd'''
设置单元格样式
'''def set_style(name,height,bold=False):
style = xlwt.XFStyle() # 初始化样式
font = xlwt.Font() # 为样式创建字体
font.name = name # 'Times New Roman'
font.bold = bold
#f.underline= Font.UNDERLINE_DOUBLE
font.color_index = 4
font.height = heightstyle.font = font # style.borders = borders</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders= xlwt.Borders()</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.left= 6</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.right= 6</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.top= 6</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> borders.bottom= 6</span>
<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> style
#读 excel
def read_excel():
# 打开文件
workbook = xlrd.open_workbook(r'F:\demo.xlsx')
# 获取所有 sheet
print workbook.sheet_names() # [u'sheet1', u'sheet2']
sheet2_name = workbook.sheet_names()[1]</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 根据sheet索引或者名称获取sheet内容</span> sheet2 = workbook.sheet_by_index(1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">sheet索引从0开始</span> sheet2 = workbook.sheet_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sheet2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sheet的名称,行数,列数</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> sheet2.name,sheet2.nrows,sheet2.ncols </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取整行和整列的值(数组)</span> rows = sheet2.row_values(3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取第四行内容</span> cols = sheet2.col_values(2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取第三列内容</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> rows </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> cols </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取单元格内容</span> <span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.cell(1,0).value.encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.cell_value(1,0).encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.row(1)[0].value.encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取单元格内容的数据类型</span> <span style="color: rgba(0, 0, 255, 1)">print</span> sheet2.cell(1<span style="color: rgba(0, 0, 0, 1)">,0).ctype
#写 excel
def write_excel():
f = xlwt.Workbook() #创建工作簿<span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)"> 创建第一个sheet: sheet1 </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)"> sheet1 </span>= f.add_sheet(u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sheet1</span><span style="color: rgba(128, 0, 0, 1)">'</span>,cell_overwrite_ok=True) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">创建sheet</span> row0 = [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">业务</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">状态</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">上海</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">广州</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">深圳</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">状态小计</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">合计</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">] column0 </span>= [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">机票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">船票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">火车票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">汽车票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">其它</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">] status </span>= [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">预订</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">出票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">退票</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">业务小计</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">] </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一行</span> <span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(row0)): sheet1.write(0,i,row0[i],set_style(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">,True)) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一列和最后一列(合并4行)</span> i, j = 1<span style="color: rgba(0, 0, 0, 1)">, 0 </span><span style="color: rgba(0, 0, 255, 1)">while</span> i < 4*len(column0) <span style="color: rgba(0, 0, 255, 1)">and</span> j <<span style="color: rgba(0, 0, 0, 1)"> len(column0): sheet1.write_merge(i,i</span>+3,0,0,column0[j],set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Arial</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220,True)) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">第一列</span> sheet1.write_merge(i,i+3,7,7) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">最后一列"合计"</span> i += 4<span style="color: rgba(0, 0, 0, 1)"> j </span>+= 1<span style="color: rgba(0, 0, 0, 1)"> sheet1.write_merge(</span>21,21,0,1,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">合计</span><span style="color: rgba(128, 0, 0, 1)">'</span>,set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">,True)) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第二列</span> i =<span style="color: rgba(0, 0, 0, 1)"> 0 </span><span style="color: rgba(0, 0, 255, 1)">while</span> i < 4*<span style="color: rgba(0, 0, 0, 1)">len(column0): </span><span style="color: rgba(0, 0, 255, 1)">for</span> j <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(status)): sheet1.write(j</span>+i+1,1<span style="color: rgba(0, 0, 0, 1)">,status[j]) i </span>+= 4 <span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)"> 创建第二个sheet: sheet2 </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)"> sheet2 </span>= f.add_sheet(u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sheet2</span><span style="color: rgba(128, 0, 0, 1)">'</span>,cell_overwrite_ok=True) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">创建sheet</span> row0 = [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓名</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">年龄</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">出生日期</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">爱好</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">关系</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">] column0 </span>= [u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小杰</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小胖</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小明</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">大神</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">大仙</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小敏</span><span style="color: rgba(128, 0, 0, 1)">'</span>,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">无名</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">] </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一行</span> <span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(row0)): sheet2.write(0,i,row0[i],set_style(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">,True)) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成第一列</span> <span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> range(0,len(column0)): sheet2.write(i</span>+1,0,column0[i],set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Times New Roman</span><span style="color: rgba(128, 0, 0, 1)">'</span>,220<span style="color: rgba(0, 0, 0, 1)">)) sheet2.write(</span>1,2,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1991/11/11</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) sheet2.write_merge(</span>7,7,2,4,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">暂无</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">合并列单元格</span> sheet2.write_merge(1,2,4,4,u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">好朋友</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">合并行单元格</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 插入图片</span> sheet2.insert_bitmap(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">F:\1.bmp</span><span style="color: rgba(128, 0, 0, 1)">'</span>,10,2<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 添加超链接</span> n= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">HYPERLINK</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> sheet2.write_merge(</span>9,9,2,8, xlwt.Formula(n +<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">("http://www.cnblogs.com/zhoujie";"jzhou\'s blog")</span><span style="color: rgba(128, 0, 0, 1)">'</span>),set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Arial</span><span style="color: rgba(128, 0, 0, 1)">'</span>,300<span style="color: rgba(0, 0, 0, 1)">,True)) sheet2.write_merge(</span>10,10, 2, 8, xlwt.Formula(n +<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">("mailto:zhoujie0111@126.com";"contact me")</span><span style="color: rgba(128, 0, 0, 1)">'</span>),set_style(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Arial</span><span style="color: rgba(128, 0, 0, 1)">'</span>,300<span style="color: rgba(0, 0, 0, 1)">,True)) f.save(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">demo1.xlsx</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">保存文件</span>
if name == 'main':
read_excel()
write_excel()
参考:http://www.cnblogs.com/snake-hand/p/3153158.html