• Python编程-Office操作-操作Excel(中)



    例子文件如下:

    一些复杂的读取操作
    getCells.py

    import openpyxl
    
    wb = openpyxl.load_workbook('example.xlsx')
    sheet = wb.get_sheet_by_name('Sheet1')
    
    print(sheet.cell(row=1, column=2).value)
    # from 1 to 8 step is 2
    for i in range(1, sheet.max_row + 1, 2):
        print(i, sheet.cell(row=i, column=2).value)
    
    print('------------------------------------------------')
        
    # enumerate range cells
    for rowOfCellObjects in sheet['A1':'C3']:
        for cellObj in rowOfCellObjects:
            print(cellObj.coordinate, cellObj.value)
        print('**************************************')
    
    print('------------------------------------------------')
    
    # enumerate the whole sheet
    for i in range(1, sheet.max_row + 1):
        for j in range(1, sheet.max_column + 1):
            print(i, sheet.cell(row=i, column=j).coordinate, sheet.cell(row=i, column=j).value)
        print('**************************************')

    运行结果:

    Apples
    1 Apples
    3 Pears
    5 Apples
    7 Strawberries
    ------------------------------------------------
    A1 2015-04-05 13:34:02
    B1 Apples
    C1 73
    **************************************
    A2 2015-04-05 03:41:23
    B2 Cherries
    C2 85
    **************************************
    A3 2015-04-06 12:46:51
    B3 Pears
    C3 14
    **************************************
    ------------------------------------------------
    1 A1 2015-04-05 13:34:02
    1 B1 Apples
    1 C1 73
    **************************************
    2 A2 2015-04-05 03:41:23
    2 B2 Cherries
    2 C2 85
    **************************************
    3 A3 2015-04-06 12:46:51
    3 B3 Pears
    3 C3 14
    **************************************
    4 A4 2015-04-08 08:59:43
    4 B4 Oranges
    4 C4 52
    **************************************
    5 A5 2015-04-10 02:07:00
    5 B5 Apples
    5 C5 152
    **************************************
    6 A6 2015-04-10 18:10:37
    6 B6 Bananas
    6 C6 23
    **************************************
    7 A7 2015-04-10 02:40:46
    7 B7 Strawberries
    7 C7 98
    **************************************

  • 相关阅读:
    基于python+django+mysql的接口测试平台
    firefox没有装在C盘,webdriver启动firefox时报错
    Python知识点面试题
    Python面试题整理
    Python程序猿面试杂谈
    Python面试-websocket及web框架
    Python面试-DB相关
    Python面试简介及并行并发
    flink-demo2
    flink-table demo
  • 原文地址:https://www.cnblogs.com/davidgu/p/4994023.html
Copyright © 2020-2023  润新知