python操作excel常用的方法

读操作模块安装
  pip install xlrd

写操作模块安装
  pip install xlwt

xlrd读操作

1、打开excel
  xl = xlrd.open_workbook('test.xls')

2、通过索引获取工作表
  table = xl.sheet()[0] #有多个sheet,获取第一个sheet

3、获取第一行的内容
  row = table.row_values(0)

4、获取第一列的内容
  col = table.col_values(0)

5、获取行数、列数
  table.nrows
  table.ncols

6、单元格的值
  table.cell(0,0).value

7、日期、合并单元格的值
  合并的单元格的值默认可以从最顶层的行获取
  获取日期
  xlrd.xldate_as_tuple(table.cell_values(1,4),xl.datemode)

8、type类型
  0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

示例代码:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import xlrd
 5 
 6 # 打开Excel文件
 7 workbook = xlrd.open_workbook('test.xlsx')
 8 
 9 # 输出Excel文件中所有sheet的名字
10 print workbook.sheet_names()
11 
12 # 根据sheet索引或者名称获取sheet内容
13 Data_sheet = workbook.sheets()[0]
14 CdfData_sheet = workbook.sheet_by_index(1)
15 Charts_sheet = workbook.sheet_by_name(u'Charts')
16 
17 # 获取sheet名称、行数和列数
18 print Data_sheet.name, Data_sheet.nrows, Data_sheet.ncols, 
19     CdfData_sheet.name, CdfData_sheet.nrows, CdfData_sheet.ncols, 
20     Charts_sheet.name, Charts_sheet.nrows, Charts_sheet.ncols
21 
22 # 获取整行和整列的值(列表
23 rows = Data_sheet.row_values(0)  # 获取第一行内容
24 cols = Data_sheet.col_values(1)  # 获取第二列内容
25 # print rows
26 # print cols
27 
28 # 获取单元格内容
29 cell_A1 = Data_sheet.cell(0, 0).value
30 cell_C1 = Data_sheet.cell(0, 2).value
31 cell_B1 = Data_sheet.row(0)[1].value
32 cell_D2 = Data_sheet.col(3)[1].value
33 print cell_A1, cell_B1, cell_C1, cell_D2
34 
35 # 获取单元格内容的数据类型
36 # ctype:0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
37 print 'cell(0,0)数据类型:', Data_sheet.cell(0, 0).ctype
38 print 'cell(1,0)数据类型:', Data_sheet.cell(1, 0).ctype
39 print 'cell(1,1)数据类型:', Data_sheet.cell(1, 1).ctype
40 print 'cell(1,2)数据类型:', Data_sheet.cell(1, 2).ctype
41 
42 # 获取单元格内容为日期的数据
43 date_value = xlrd.xldate_as_tuple(Data_sheet.cell_value(1, 0), workbook.datemode)
44 print date_value
45 print '%d:%d:%d' % (date_value[3:])
46 
47 d = {'11:25:59': [1, 2, 3], '11:26:00': [2, 3, 4], '11:26:01': [3, 4, 5]}
48 print d['11:25:59']
49 print d['11:26:00']
50 print d['11:26:01']

xlrd写操作
1、创建excel
  xl = xlwt.Workbook()

2、创建工作表
  table = xl.add_sheet(name)

3、写内容
  table.write(row,col,cell)

4、写入并设置单元格式
  table.write(row,col,cell,style)

5、合并单元格写入
  table.write_merge(x,x+n,y,y+m,cell)

6、保存单元格
  Workbook.save('demo.xls')

示例代码:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import xlwt
 5 
 6 def set_style(name, height, bold=False):
 7     style = xlwt.XFStyle()
 8 
 9     font = xlwt.Font()
10     font.name = name
11     font.bold = bold
12     font.color_index = 4
13     font.height = height
14 
15     style.font = font
16     return style
17 
18 def write_excel():
19     # 创建工作簿
20     workbook = xlwt.Workbook(encoding='utf-8')
21     # 创建sheet
22     data_sheet = workbook.add_sheet('demo')
23     row0 = [u'字段名称', u'时间', 'CRNTI', 'CELL-ID']
24     row1 = [u'测试字段', '15:50:33-15:52:14', 22706, 4190202]
25 
26     # 生成第一行和第二行
27     for i in range(len(row0)):
28         data_sheet.write(0, i, row0[i], set_style('Times New Roman', 220, True))
29         data_sheet.write(1, i, row1[i], set_style('Times New Roman', 220, True))
30 
31     workbook.save('demo.xls')
32 
33 if __name__ == '__main__':
34     write_excel()
原文地址:https://www.cnblogs.com/gide/p/8573319.html