300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > [转载] Python利用openpyxl模块读取excel文件内容

[转载] Python利用openpyxl模块读取excel文件内容

时间:2020-10-24 05:57:09

相关推荐

[转载] Python利用openpyxl模块读取excel文件内容

参考链接: Python | 使用openpyxl模块调整Excel文件的行和列

import openpyxl

wb = openpyxl.load_workbook('原始数据.xlsx')

#读取sheet工作表名

print(wb.sheetnames)

ws = wb.active

#打印工作表行列数

print("表格共%d行,列数共%d列" %(ws.max_row,ws.max_column))

#读取某个单元格的坐标和值

print('--------读取A2单元格的坐标和值--------')

cellObject = ws['A2']

print(cellObject.value,cellObject.coordinate)

print('--------读取A2单元格的值--------')

print(ws.cell(row=2,column=1).value)

#读取某一行的数据

print('-------读取第2行列的数据,方式1:--------')

row_num = ws['2']

for cell in row_num:

print(cell.value)

print('-------读取第2行列的数据,方式2:--------')

for i in range (1,7):

print(i,ws.cell(row=2,column=i).value)

#读取某一列的数据

print('-------读取A列的数据,方式1:-------')

column_num = ws['A']

for cell in column_num:

print(cell.value)

print('-------读取A列的数据,方式2:-------')

for i in range (1,4):

print(i,ws.cell(row=i,column=1).value)

#按行读取每一行的数据

print('-------按行读取每一行的数据-------')

row_range = ws['1:3']

for rowObject in row_range:

for columnObject in rowObject:

print(columnObject.value)

print('-------End of Row-------')

# 按列读取每一列数据

print('-------按列读取每一列的数据-------')

colunm_range = ws['A:F']

for columnObject in colunm_range:

for rowObject in columnObject:

print(rowObject.value)

print('-------End of Column-------')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。