Python读写Excel表格的方法
这篇文章主要为大家详细介绍了Python读写Excel表格的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Python读写Excel表格的具体代码,供大家参考,具体内容如下
python读取Excel表格:
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 42 43 44 45 46 47 48 49 50 51 52 53 | import xlrd def read_excel(): # 打开文件 wb = xlrd.open_workbook(r 'test.xls' ) # 获取所有sheet的名字 print (wb.sheet_names()) # 获取第二个sheet的表名 sheet2 = wb.sheet_names()[ 1 ] print ( "sheet2 = {}" . format (sheet2)) # sheet1索引从0开始,得到sheet1表的句柄 sheet1 = wb.sheet_by_index( 0 ) rowNum = sheet1.nrows colNum = sheet1.ncols print ( "rowNum = {}, colNum = {}" . format (rowNum, colNum)) # 获取某一个位置的数据 c1_0 = sheet1.cell( 1 , 0 ).value print ( "c1_0 = {}" . format (c1_0)) # 1 ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error print (sheet1.cell( 1 , 2 ).ctype) # 获取整行和整列的数据 # 第二行数据 row2 = sheet1.row_values( 1 ) print ( "row2 = {}" . format (row2)) # 第二列数据 cols2 = sheet1.col_values( 2 ) print ( "cols2 = {}" . format (cols2)) # python读取excel中单元格内容为日期的方式 # 返回类型有5种 print ( "for循环:" ) for i in range (rowNum): # if sheet1.cell(i, 2).ctype == 1: # d = xlrd.xldate_as_tuple(sheet1.cell_value(i, 2), wb.datemode) # print(date(*d[:3]), end='') print (sheet1.cell(i, 2 )) # 输出如下: # ['我的第一个表', '第二个', '呵呵第三个'] # sheet2 = 第二个 # rowNum = 8, colNum = 3 # c1_0 = w # 2 # row2 = ['w', 's', 10.0] # cols2 = ['z', 10.0, 666.0, '2021年2月25日 02:06:25', 44252.0, 'x', 1, ''] # for循环: # text:'z' # number:10.0 # number:666.0 # text:'2021年2月25日 02:06:25' # xldate:44252.0 # text:'x' # bool:1 # empty:'' |
python写入Excel表格:
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 42 43 44 45 46 | import xlwt # 写入数据 def write_excel(): f = xlwt.Workbook() # 创建表sheet1 sheet1 = f.add_sheet(u 'sheet1' , cell_overwrite_ok = True ) # 如果是写入中文,则要用u'汉字'的形式。比如 sheet1.write(0,0, u'汉字') row0 = [u '业务' , u '状态' , u '北京' , u '上海' , u '广州' , u '深圳' , u '状态小计' , u '合计' ] column0 = [u '机票' , u '船票' , u '火车票' , u '汽车票' , u '其他' ] status = [u '预定' , u '出票' , u '退票' , u '业务小计' ] for i in range ( 0 , len (row0)): sheet1.write( 0 , i, row0[i], set_style( "Time New Roman" , 220 , True )) # 合并单元格: # sheet1.write_merge(x, x + m, y, y + n, string, style) # x表示行,y表示列,m表示跨行个数,n表示跨列个数,string表示要写入的单元格内容,style表示单元格样式。 i, j = 1 , 0 while i < 4 * len (column0): # 控制循环:每次加4 # 第一列 sheet1.write_merge(i, i + 3 , 0 , 0 , column0[j], set_style( 'Arial' , 220 , True )) # 最后一列 sheet1.write_merge(i, i + 3 , 7 , 7 ) i + = 4 j + = 1 sheet1.write_merge( 21 , 21 , 0 , 1 , u '合计' , set_style( "Time New Roman" , 220 , True )) i = 0 while i < 4 * len (column0): # 控制外层循环:每次加4 for j in range ( 0 , len (status)): # 控制内层循环:设置每一行内容 sheet1.write(i + j + 1 , 1 , status[j]) i + = 4 # 创建sheet2 sheet2 = f.add_sheet(u 'sheet2' ,cell_overwrite_ok = True ) row0 = [u '姓名' , u '年龄' , u '出生日期' , u '爱好' , u '关系' ] column0 = [u 'UZI' , u 'Faker' , u '大司马' , u 'PDD' , u '冯提莫' ] # 生成第一行 for i in range ( 0 , len (row0)): sheet2.write( 0 , i, row0[i], set_style( 'Times New Roman' , 220 , True )) # 生成第一列 for i in range ( 0 , len (column0)): sheet2.write(i + 1 , 0 , column0[i], set_style( 'Times New Roman' , 220 , True )) f.save( 'data.xls' ) |
执行上面这个写入excel表格的函数后,会生成data.xls文件。
写入表格1:
写入表格2:
以上就是本文的全部内容,希望对大家的学习有所帮助