python关于excel读写(xlrd和xlwt ,openpyxl,pandas)
Python作为一种脚本语言相较于shell具有更强大的文件处理能力,一般shell在处理纯文本文件时较为实用,而对特殊文件的处理如excel表格则Python会更得心应手,主要体现在它可以调用很多第三方功能包来实现我们想要的功能,Python读写excel的方式有很多,不同的模块在读写的讲法上稍有区别:用xlrd和xlwt进行excel读写;用openpyxl进行excel读写;用pandas进
·
Python作为一种脚本语言相较于shell具有更强大的文件处理能力,一般shell在处理纯文本文件时较为实用,而对特殊文件的处理如excel表格则Python会更得心应手,主要体现在它可以调用很多第三方功能包来实现我们想要的功能,Python读写excel的方式有很多,不同的模块在读写的讲法上稍有区别: 用xlrd和xlwt进行excel读写; 用openpyxl进行excel读写; 用pandas进行excel读写; 为了方便演示,我这里新建了一个data.xlsx文件,第一个工作表sheet1区域“A1:C19”的内容如下,用于测试读excel的代码: Update Date AP Version MD Version 2018/6/25 alps-mp-o1.mp1-V1.132.2 MOLY.LR12A.R2.MP.V34.15---MOLY.LR12A.R3.MP.V14.4 2018/6/11 alps-mp-o1.mp1-V1.122 MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2 2018/5/29 alps-mp-o1.mp1-V1.112 MOLY.LR12A.R2.MP.V30.8---MOLY.LR12A.R3.MP.V10.3 2018/5/15 alps-mp-o1.mp1-V1.102 MOLY.LR12A.R2.MP.V28.1---MOLY.LR12A.R3.MP.V8 2018/5/2 alps-mp-o1.mp1-V1.92 MOLY.LR12A.R2.MP.V26.1--MOLY.LR12A.R3.MP.V6 2018/4/17 alps-mp-o1.mp1-V1.82 MOLY.LR12A.R2.MP.V24.1 2018/4/13 alps-mp-o1.mp1-V1.74 MOLY.LR12A.R2.MP.V22.1 2018/3/29 alps-mp-o1.mp1-V1.69 MOLY.LR12A.R2.MP.V21 2018/3/20 alps-mp-o1.mp1-V1.64 MOLY.LR12A.R2.MP.V20.1 2018/3/15 alps-mp-o1.mp1-V1.59 MOLY.LR12A.R2.MP.V19 2018/3/7 alps-mp-o1.mp1-V1.54 MOLY.LR12A.R2.MP.V18 2018/2/27 alps-mp-o1.mp1-V1.42 MOLY.LR12A.R2.MP.V15 2018/2/8 alps-mp-o1.mp1-V1.37 MOLY.LR12A.R2.MP.V14 2018/2/1 alps-mp-o1.mp1-V1.30 MOLY.LR12A.R2.MP.V13 2018/1/25 alps-mp-o1.mp1-V1.24.1 MOLY.LR12A.R2.MP.V12.6 2018/1/12 alps-mp-o1.mp1-V1.12 MOLY.LR12A.R2.MP.V10.6 2018/1/9 alps-mp-o1.mp1-V1.4 MOLY.LR12A.R2.MP.V9 2017/12/26 alps-mp-o1.mp1-V1 MOLY.LR12A.R2.MP.V8.6 首先是安装第三方模块xlrd和xlwt,直接输入命令"sudo pip install xlrd"和"sudo pip install xlwt"就行,如下:1 利用xlrd和xlwt进行excel读写(xlwt不支持xlsx) 1.1 xlrd读excel # encoding: utf-8 import xlrd book = xlrd.open_workbook('data.xlsx') #sheet1 = book.sheet_by_index(1) #索引的方式,从0开始 #sheet1 = book.sheet_by_name('sheet2') #名字的方式 sheet1 = book.sheets()[0] #第三种方式 nrows = sheet1.nrows print u'表格总行数 ',nrows ncols = sheet1.ncols print u'表格总列数 ',ncols row3_values = sheet1.row_values(2) print u'第3行值 ',row3_values col3_values = sheet1.col_values(2) print u'第3列值 ',col3_values cell_3_3 = sheet1.cell(2,2).value print u'第3行第3列的单元格的值:',cell_3_3 运行结果 表格总行数 19 表格总列数 3 第3行值 [u'2018/6/11', u'alps-mp-o1.mp1-V1.122', u'MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2'] 第3列值 [u'MD\u7248\u672c', u'MOLY.LR12A.R2.MP.V34.15---MOLY.LR12A.R3.MP.V14.4', u'MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2', u'MOLY.LR12A.R2.MP.V30.8---MOLY.LR12A.R3.MP.V10.3', u'MOLY.LR12A.R2.MP.V28.1---MOLY.LR12A.R3.MP.V8', u'MOLY.LR12A.R2.MP.V26.1--MOLY.LR12A.R3.MP.V6', u'MOLY.LR12A.R2.MP.V24.1', u'MOLY.LR12A.R2.MP.V22.1', u'MOLY.LR12A.R2.MP.V21', u'MOLY.LR12A.R2.MP.V20.1', u'MOLY.LR12A.R2.MP.V19', u'MOLY.LR12A.R2.MP.V18', u'MOLY.LR12A.R2.MP.V15', u'MOLY.LR12A.R2.MP.V14', u'MOLY.LR12A.R2.MP.V13', u'MOLY.LR12A.R2.MP.V12.6', u'MOLY.LR12A.R2.MP.V10.6', u'MOLY.LR12A.R2.MP.V9', u'MOLY.LR12A.R2.MP.V8.6'] 第3行第3列的单元格的值: MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2 1.2 xlwt写excel 主要代码如下,很简单: import xlwt # 不支持excel2007的xlsx格式 workbook = xlwt.Workbook() worksheet = workbook.add_sheet('test') worksheet.write(0,0,'A1data') workbook.save('excelwrite.xls') 程序运行后,新建excelwrite.xls工作簿并插入text工作表,A1的内容为A1data。 2 利用openpyxl读写excel,注意这里只能是xlsx类型的excel 安装的话,直接输入命令"sudo pip install openpyxl"就行,很快就能安装完毕。 2.1 读excel # encoding: utf-8 from openpyxl.reader.excel import load_workbook workbook = load_workbook('data.xlsx') worksheet = workbook.worksheets[0] row3=[item.value for item in list(worksheet.rows)[2]] print u'第3行值',row3 col3=[item.value for item in list(worksheet.columns)[2]] print u'第3行值',col3 cell_2_3=worksheet.cell(row=2,column=3).value print u'第2行第3列值',cell_2_3 max_row=worksheet.max_row print u'最大行',max_row 运行结果: 第3行值 [u'2018/6/11', u'alps-mp-o1.mp1-V1.122', u'MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2'] 第3行值 [u'MD Version', u'MOLY.LR12A.R2.MP.V34.15---MOLY.LR12A.R3.MP.V14.4', u'MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2', u'MOLY.LR12A.R2.MP.V30.8---MOLY.LR12A.R3.MP.V10.3', u'MOLY.LR12A.R2.MP.V28.1---MOLY.LR12A.R3.MP.V8', u'MOLY.LR12A.R2.MP.V26.1--MOLY.LR12A.R3.MP.V6', u'MOLY.LR12A.R2.MP.V24.1', u'MOLY.LR12A.R2.MP.V22.1', u'MOLY.LR12A.R2.MP.V21', u'MOLY.LR12A.R2.MP.V20.1', u'MOLY.LR12A.R2.MP.V19', u'MOLY.LR12A.R2.MP.V18', u'MOLY.LR12A.R2.MP.V15', u'MOLY.LR12A.R2.MP.V14', u'MOLY.LR12A.R2.MP.V13', u'MOLY.LR12A.R2.MP.V12.6', u'MOLY.LR12A.R2.MP.V10.6', u'MOLY.LR12A.R2.MP.V9', u'MOLY.LR12A.R2.MP.V8.6'] 第2行第3列值 MOLY.LR12A.R2.MP.V34.15---MOLY.LR12A.R3.MP.V14.4 最大行 19 2.2 写excel 主要代码如下: import openpyxl workbook = openpyxl.Workbook() sheet=workbook.active sheet['A1']='hi,wyu' workbook.save('new.xlsx') 程序运行后,新建new.xls工作簿并插入sheet工作表,A1的内容为hi,wyu。 3 利用pandas读取excel Pandas的名称来自于面板数据(panel data)和python数据分析(data analysis)。 首先要安装pandas模块,用sudo pip install pandas安装。 pandas是一个数据处理的包,本身提供了许多读取文件的函数,像read_csv(读取csv文件),read_excel(读取excel文件)等,只需一行代码就能实现文件的读取 3.1 读excel 主要代码如下,就1行代码搞定,注意这里的data是DataFrame类型: import pandas as pd df = pd.read_excel(r'data.xlsx',sheetname=0) print(df) 运行结果: Update Date AP Version MD Version 0 2018/6/25 alps-mp-o1.mp1-V1.132.2 MOLY.LR12A.R2.MP.V34.15---MOLY.LR12A.R3.MP.V14.4 1 2018/6/11 alps-mp-o1.mp1-V1.122 MOLY.LR12A.R2.MP.V32.5---MOLY.LR12A.R3.MP.V12.2 2 2018/5/29 alps-mp-o1.mp1-V1.112 MOLY.LR12A.R2.MP.V30.8---MOLY.LR12A.R3.MP.V10.3 3 2018/5/15 alps-mp-o1.mp1-V1.102 MOLY.LR12A.R2.MP.V28.1---MOLY.LR12A.R3.MP.V8 4 2018/5/2 alps-mp-o1.mp1-V1.92 MOLY.LR12A.R2.MP.V26.1--MOLY.LR12A.R3.MP.V6 5 2018/4/17 alps-mp-o1.mp1-V1.82 MOLY.LR12A.R2.MP.V24.1 6 2018/4/13 alps-mp-o1.mp1-V1.74 MOLY.LR12A.R2.MP.V22.1 7 2018/3/29 alps-mp-o1.mp1-V1.69 MOLY.LR12A.R2.MP.V21 8 2018/3/20 alps-mp-o1.mp1-V1.64 MOLY.LR12A.R2.MP.V20.1 9 2018/3/15 alps-mp-o1.mp1-V1.59 MOLY.LR12A.R2.MP.V19 10 2018/3/7 alps-mp-o1.mp1-V1.54 MOLY.LR12A.R2.MP.V18 11 2018/2/27 alps-mp-o1.mp1-V1.42 MOLY.LR12A.R2.MP.V15 12 2018/2/8 alps-mp-o1.mp1-V1.37 MOLY.LR12A.R2.MP.V14 13 2018/2/1 alps-mp-o1.mp1-V1.30 MOLY.LR12A.R2.MP.V13 14 2018/1/25 alps-mp-o1.mp1-V1.24.1 MOLY.LR12A.R2.MP.V12.6 15 2018/1/12 alps-mp-o1.mp1-V1.12 MOLY.LR12A.R2.MP.V10.6 16 2018/1/9 alps-mp-o1.mp1-V1.4 MOLY.LR12A.R2.MP.V9 17 2017/12/26 alps-mp-o1.mp1-V1 MOLY.LR12A.R2.MP.V8.6 3.2 写excel 主要代码如下: # encoding: utf-8 from pandas import DataFrame data={ 'name':[u'张三',u'李四',u'王五'], 'age':[21,22,23], 'sex':[u'男',u'女',u'男'] } df=DataFrame(data) df.to_excel('new.xlsx') 程序运行后,将新建(或替换)new.xlsx文件,并在工作表sheet1的A1:D4区域中保存内容如下: age name sex 0 21 张三 男 1 22 李四 女 2 23 王五 男 至此,我们就完成了excel的读写。总的来说,这3种方法都很简单,尤其是第3种方法,1行代码就搞定,在数据处理中,经常会用到pandas这个包(该包可以处理众多常见的数据存储格式,如CSV、JSON等),功能很强大,当然还有许多其他的包也可以完成excel的读写。
备注BWF:
xlrd:
import os import xlrd # 获取当前.py文件所在路径 filepath = os.getcwd() print(filepath) excelfilepath = filepath + r"\testdata.xlsx" # 组织excel文件完整路径为一个字符串 print(excelfilepath) e = xlrd.open_workbook(excelfilepath) # 打开excel文件,得到excel文件的对象 sheet0 = e.sheet_by_index(0) # 通过sheet页的索引获取sheet页的对象,sheet索引从左往右从0开始依次递增 nrows_number = sheet0.nrows # 获取数据的行数 print(nrows_number) ncols_number = sheet0.ncols # 获取数据的列数 print(ncols_number) # 使用for循环读取每一行的内容 for i in range(nrows_number): values = sheet0.row_values(i) #读取第i行的数据,i从0开始 print(values) print("***************************************************") # 使用for循环读取每一列的内容 for i in range(ncols_number): values = sheet0.col_values(i) #读取的是第i列的数据,i从0开始 print(values) print("***************************************************") # 按照单元格坐标读取 # 读取第一行第一列交叉单元格内容 values = sheet0.cell_value(0, 0) print(values) # 读取第三行第二列交叉单元格内容 values = sheet0.cell_value(2, 1) print(values)
xlwt:
import xlwt import os # 创建一个excel文件对象,声明该文件以utf-8编码 e = xlwt.Workbook(encoding='utf-8') #创建excel文件的sheet页对象 s = e.add_sheet("test", cell_overwrite_ok=True) # test为创建的sheet页的名字,cell_overwrite_ok表示是否可以覆盖单元格内容 # 向sheet页对象s添加数据 s.write(0, 0, "重启是个好发明") # 向第一行第一列单元格写入数据 # 使用for循环控制单元格坐标的变化,向excel中写入数据 for i in range(1, 10): for j in range(1, i+1): s.write(i, j, "%d*%d=%d"%(j,i,(j*i))) excelfilepath = os.getcwd() + r"\testdata2.xls" e.save(excelfilepath) # 将内容保存到excelfilepath文件中
更多推荐
所有评论(0)