python读取xlsx文件

# encoding: UTF-8
from openpyxl import load_workbook

workbook = load_workbook(u'xxxxxx.xlsx')    #相对路径,找到需要打开的文件位置
booksheet = workbook.active                 #获取当前活跃的sheet,默认是第一个sheet

#如果想获取别的sheet页采取下面这种方式,先获取所有sheet页名,在通过指定那一页。
# sheets = workbook.get_sheet_names()  # 从名称获取sheet
# booksheet = workbook.get_sheet_by_name(sheets[0])

#获取sheet页的行数据
rows = booksheet.rows
#获取sheet页的列数据
columns = booksheet.columns


i = 0
# 迭代所有的行
for row in rows:
    i = i + 1
    line = [col.value for col in row]
    cell_datetime = booksheet.cell(row=i, column=1).value               #获取第i1列的数据
    cell_data = booksheet.cell(row=i, column=2).value                   #获取第i2l列的数据


# 通过坐标读取值
# cell_11 = booksheet.cell('A1').value
# cell_11 = booksheet.cell(row=1, column=1).value
# print cell_11

猜你喜欢

转载自blog.csdn.net/qq_769932247/article/details/80415254