本文介绍以下使用python库实现对excel的读写。主要思路就是通过xlrd 读取excel数据进行处理、通过openpyxl 进行数据的写入。 (python有很多处理excel的库,这里只是记录其中一种方式。后面会系统学习pandas)
参考:https://www.jb51.net/article/165629.htm https://www.jb51.net/article/205141.htm#_label0
xlrd的使用
安装
在pycharm中的控制台输入:pip install xlrd
即可
操作流程
导入模块
打开工作簿
1 2 workbook = xlrd.open_workbook(filename=r'C:\Users\Windows10\Desktop\xlsx文件.xlsx' )
获取需要操作的sheet表格(有三种方法)
1 2 3 4 5 6 7 8 通过索引获取:-------------- table = workbook.sheets()[0 ] 通过sheet名称获取:--------- table = workbook.sheet_by_name(sheet_name='Sheet1' )
补充:获取excel所有sheel名称
1 2 names = workbook.sheet_names()
行列操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 row = table.nrows print (row)col = table.ncols print (col)num = table.row_len(0 ) print (num)table_list = table.row_values(rowx=0 , start_colx=0 , end_colx=None ) print (table_list)table_list = table.col_values(colx=0 , start_rowx=0 , end_rowx=None ) print (table_list)print (table.row(0 )) print (table.row_slice(0 )) print (table.row_types(0 , start_colx=0 , end_colx=None )) print (table.col(0 , start_rowx=0 , end_rowx=None )) print (table.col_slice(0 , start_rowx=0 , end_rowx=None )) print (table.col_types(0 , start_rowx=0 , end_rowx=None ))
单元格操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 value = table.cell_value(rowx=0 , colx=1 ) print (value)value = table.cell(rowx=0 , colx=1 ) print (value)value = table.cell_type(rowx=0 , colx=1 ) print (value)
openpyxl的使用 特点概述
openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间转换容易 注意:如果文字编码是“gb2312” 读取后就会显示乱码,请先转成Unicode。
起始值:1,1
openpyxl 支持直接横纵坐标访问,如A1,B2..
具体操作
先理清openpyxl的操作对象
workbook
: 工作簿,一个excel文件包含多个sheet。
sheet
:工作表,一个workbook有多个,表名识别,如“sheet1”,“sheet2”等。
cell
: 单元格,存储数据对象
新建表
1 2 3 4 5 6 7 8 9 10 11 12 13 wb = Workbook(encoding='UTF-8' ) ws = wb.active ws = wb.create_sheet("Mysheet" ) ws = wb.create_sheet("Mysheet" , 0 ) ws.title = "New Title" ws = wb.create_sheet(title="Pip" )
注:也可以打开已有的excel文件
1 2 xfile = openpyxl.load_workbook('D:\\Tencent Files\\95239002\\聚类求和.xlsx' ) sheet = xfile.get_sheet_by_name("总的数据" )
单元格赋值
1 2 3 4 sheet.cell(row=2 ,column=5 ).value=99 sheet.cell(row=3 ,column=5 ,value=100 ) ws['A4' ] = 4
逐行写
1 2 3 4 5 ws.append(iterable) ws.append([‘This is A1', ‘This is B1' , ‘This is C1']) ws.append({‘A' : ‘This is A1', ‘C' : ‘This is C1'}) ws.append({1 : ‘This is A1' , 3 : ‘This is C1'})
读表操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 from openpyxl import load_workbookexcel=load_workbook('E:/test.xlsx' ) table = excel.get_sheet_by_name('Sheet1' ) rows=table.max_row cols=table.max_column Data=table.cell(row=row,column=col).value ws = wb["frequency" ] sheet_names = wb.get_sheet_names() ws = wb.get_sheet_by_name(sheet_names[index]) ws =wb.active wb.get_active_sheet().title ws.iter_rows(range_string=None , row_offset=0 , column_offset=0 ): range -string(string)-单元格的范围:例如('A1:C4' ) row_offset-添加行 column_offset-添加列 for row in ws.iter_rows('A1:C2' ): for cell in row: print cell rows=ws.rows columns=ws.columns print rows[n] print columns[n]
具体例子:聚类求和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import xlrd as xrimport openpyxlxlsx = xr.open_workbook("D:\\Tencent Files\\95239002\\聚类求和.xlsx" ) ret1 = xlsx.sheets()[0 ] sheet1_col1 = ret1.col_values(0 ) sheet1_col2 = ret1.col_values(1 ) sheet1_col3 = ["result" ] i, j = 1 , 2 k = 1 while True : if j<len (sheet1_col2) and sheet1_col2[i]==sheet1_col2[j]: k+=1 j+=1 continue if k==1 : sheet1_col3.append(round (sheet1_col1[i], 6 )) else : sheet1_col3.append(round (sum (sheet1_col1[i:j]), 6 )) for m in range (k-1 ): sheet1_col3.append("same" ) k = 1 i = j j = i+1 if j>=len (sheet1_col2): sheet1_col3.append(round (sheet1_col1[i], 6 )) break print (sheet1_col3)xfile = openpyxl.load_workbook('D:\\Tencent Files\\95239002\\聚类求和.xlsx' ) sheet = xfile.get_sheet_by_name("总的数据" ) for i in range (1 , len (sheet1_col3)+1 ): sheet.cell(i, 3 , sheet1_col3[i-1 ]) xfile.save("聚类求和3.xlsx" )