讀取excel表格內容
************************************************************
通過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)