美文网首页Python学习大数据 爬虫Python AI Sqldata mine
Python读写Excel表格,就是这么简单粗暴又好用

Python读写Excel表格,就是这么简单粗暴又好用

作者: 1a076099f916 | 来源:发表于2019-01-14 14:12 被阅读9次
Python读写Excel表格,就是这么简单粗暴又好用

最近在做一些数据处理和计算的工作,因为数据是以.CSV格式保存的,因此刚开始直接用Excel来处理。

但是做着做着发现重复的劳动,其实并没有多大的意义,于是就想着写个小工具帮着处理。

进群进群:700341555可以获取Python各类入门学习资料!

这是我的微信公众号【Python编程之家】各位大佬用空可以关注下,每天更新Python学习方法,感谢!

111111111111.png

以前正好在一本书上看到过,使用Python来处理Excel表格,可惜没有仔细看。

于是我到处查找资料,基本解决了日常所需,终于算是完成了任务,因此撰写此文就算是总结吧,主要记录使用过程的常见问题及解决。

Python操作Excel,主要用到xlrd和xlwt这两个库,即xlrd是读Excel,xlwt是写Excel的库。

可从这里下载https://pypi.python.org/pypi。下面分别记录Python读和写Excel。

Python读写Excel表格,就是这么简单粗暴又好用
  • Python写Excel——xlwt

Python写Excel的难点,不在构造一个Workbook的本身,而是填充的数据,不过这不在范围内。

在写Excel的操作中,也有棘手的问题,比如写入合并的单元格,就是比较麻烦的,另外写入还有不同的样式。

详细代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import xlwt

设置表格样式

def set_style(name,height,bold=False):
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = name
font.bold = bold
font.color_index = 4
font.height = height
style.font = font
return style

写Excel

def write_excel():
f = xlwt.Workbook()
sheet1 = f.add_sheet('学生',cell_overwrite_ok=True)
row0 = ["姓名","年龄","出生日期","爱好"]
colum0 = ["张三","李四","恋习Python","小明","小红","无名"]

写第一行

for i in range(0,len(row0)):
sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))

写第一列

for i in range(0,len(colum0)):
sheet1.write(i+1,0,colum0[i],set_style('Times New Roman',220,True))
sheet1.write(1,3,'2006/12/12')
sheet1.write_merge(6,6,1,3,'未知')#合并行单元格
sheet1.write_merge(1,2,3,3,'打游戏')#合并列单元格
sheet1.write_merge(4,5,3,3,'打篮球')
f.save('test.xls')
if name == 'main':
write_excel()
</pre>

结果图:

Python读写Excel表格,就是这么简单粗暴又好用

在此,对write_merge()的用法稍作解释,如上述:sheet1.write_merge(1,2,3,3,'打游戏'),即在四列合并第2,3列,合并后的单元格内容为"合计",并设置了style。其中,里面所有的参数都是以0开始计算的。

Python读写Excel表格,就是这么简单粗暴又好用
  • Python读Excel——xlrd

Python读取Excel表格,相比xlwt来说,xlrd提供的接口比较多,但过程也有几个比较麻烦的问题,比如读取日期、读合并单元格内容。

Python读写Excel表格,就是这么简单粗暴又好用

下面先看看基本的操作:

Python读写Excel表格,就是这么简单粗暴又好用

(图表数据)

整体思路为,打开文件,选定表格,读取行列内容,读取表格内数据

详细代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import xlrd
from datetime import date,datetime
file = 'test3.xlsx'
def read_excel():
wb = xlrd.open_workbook(filename=file)#打开文件
print(wb.sheet_names())#获取所有表格名字
sheet1 = wb.sheet_by_index(0)#通过索引获取表格
sheet2 = wb.sheet_by_name('年级')#通过名字获取表格
print(sheet1,sheet2)
print(sheet1.name,sheet1.nrows,sheet1.ncols)
rows = sheet1.row_values(2)#获取行内容
cols = sheet1.col_values(3)#获取列内容
print(rows)
print(cols)
print(sheet1.cell(1,0).value)#获取表格里的内容,三种方式
print(sheet1.cell_value(1,0))
print(sheet1.row(1)[0].value)
</pre>

运行结果如下:

Python读写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才能开始操作。

详细代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import xlrd
from datetime import date,datetime
print(sheet1.cell(1,2).ctype)
date_value = xlrd.xldate_as_tuple(sheet1.cell_value(1,2),wb.datemode)
print(date_value)
print(date(date_value[:3]))
print(date(
date_value[:3]).strftime('%Y/%m/%d'))
</pre>

运行结果如下:

Python读写Excel表格,就是这么简单粗暴又好用

2.获取合并单元格的内容

在操作之前,先介绍一下merged_cells()用法,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列合并。

详细代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">print(sheet1.merged_cells)
print(sheet1.cell_value(1,3))
print(sheet1.cell_value(4,3))
print(sheet1.cell_value(6,1))
</pre>

运行结果如下:

Python读写Excel表格,就是这么简单粗暴又好用

**发现规律了没?是的,获取merge_cells返回的row和col低位的索引即可! **于是可以这样批量获取:

详细代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">merge = []
print(sheet1.merged_cells)
for (rlow,rhigh,clow,chigh) in sheet1.merged_cells:
merge.append([rlow,clow])
for index in merge:
print(sheet1.cell_value(index[0],index[1]))
</pre>

运行结果跟上图一样,如下:

Python读写Excel表格,就是这么简单粗暴又好用

Python读写Excel表格,就是这么简单粗暴又好用,如果觉得不错,对你工作中有帮助,动动手指分享给更多人哦。

相关文章

网友评论

    本文标题:Python读写Excel表格,就是这么简单粗暴又好用

    本文链接:https://www.haomeiwen.com/subject/edgddqtx.html