fanlulin

************************************************************

通过python处理excel

************************************************************

1.导入python库

import  openpyxl as pxl

2.装入excel表格

book=pxl.load_workbook("./a.xlsx")#表格的地址,可以是相对地址,也可以是绝对地址

book=pxl.load_workbook("./a.xlsx",data_only=True)#单元格内公式的值被读取出来

3.取工作表

sheet = book.worksheet[0]

sheet = book.active#取活跃的工作表

sheet = book["price"]#根据工作表的名字取工作表

4.遍历所有的工作表

for sheet in book.worksheets:

  print(sheet.title)#打印工作表的名字

5.工作表的有效行号和列号

sheet.min_row               sheet.max_row                      #最小/大行号

sheet.min_column         sheet.max_column                #最小/大列号

6.按行遍历工作表

for  row in sheet.rows:

  for cell in row:

    print(cell.value)

7.遍历列名为‘G\'的列

for cell in sheet[\'G\']:

  print(cell.value)

8.遍历第3行

for cell in sheet[3]:

  print(cell.value,type(cell.value),cell.coordinate,cell.col_idx,cell.number_format)

type(cell.value)      :   int , float  ,  str  ,  datetime.datetime

cell.coordinate       :\'A2\'#坐标

cell.col_idx             :单元格的列号

cell.number_format:数的显示格式,“General”  "0.00%"  "0.00E+00"

9.遍历指定列

colRange = sheet[\'C:F\']

for col in colRange:

  for cell in col:

    print(cell.value)

10.遍历指定行

colRange = sheet[5:10]

 

 11.按行遍历左上角A1右下角A2的子表

for row in sheet[\'A1\':\'D2\']:

  for cell in row:

    print(cell.value)

12.输出指定单元格的值

print(sheet[\'C9\'].value)

print(sheet.cell(row=8,column=4).value)

分类:

技术点:

相关文章: