zoukankan      html  css  js  c++  java
  • 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
    **************************************

  • 相关阅读:
    使用Pandas groupby连接来自多行的字符串
    Pandas数据分析介绍
    SQL Server 32位数据源与64位数据源区别
    SQL Server install
    windows 远程提示CredSSP
    linux 终端下以图形界面打开当前文件夹
    Linux g++ include link
    undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
    Linux下的库操作工具-nm、ar、ldd、ldconfig和ld.so
    git update
  • 原文地址:https://www.cnblogs.com/davidgu/p/4994023.html
Copyright © 2011-2022 走看看