zoukankan      html  css  js  c++  java
  • python针对excel的读写操作-----openpyxl

    #python读取excel
    from openpyxl import load_workbook
    wb = load_workbook('test.xlsx')
    print(wb.sheetnames)

    from openpyxl import load_workbook
    wb = load_workbook('test.xlsx')
    sheet = wb.get_sheet_by_name('Sheet1')
    print(sheet['A'])#列表A的所有数据
    >>>(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>, <Cell 'Sheet1'.A7>, <Cell 'Sheet1'.A8>, <Cell 'Sheet1'.A9>, <Cell 'Sheet1'.A10>)
    print(sheet['4'])#列表第四行的所有内容

    >>>(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>, <Cell 'Sheet1'.D4>, <Cell 'Sheet1'.E4>)

    print(sheet['C4'].value)
    >>>哎哟哇

    for i in sheet['C']:
    print(i.value)

    >>>c1
       c2
       c3
       哎哟哇
       c5
       c6
       c7
       c8
       c9
       c10



    #python 写入excel
    from openpyxl import Workbook
    wb = Workbook()
    sheet = wb.active
    sheet.title = 'New sheet'#定义sheet的标题,下面的sheet名字
    sheet['C3'] = 'hello'#C列,第三行,写入hello
    for i in range(10):
        sheet['A%d' %(i+1)].value = i+1#循环遍历0到9,然后i+1 就是1到10,这句话表示A1-A10,写入1-10
    sheet['E1'].value = '=SUM(A:A)'#在E1写入A列所有数据的和
    wb.save('保存一个新的excel.xlsx')#保存到一个新的excel

  • 相关阅读:
    aa
    ECS上搭建Docker(CentOS7)
    mysql时间戳转日期
    rsync用法
    docker安装mysql8
    使用Docker安装mysql,挂载外部配置和数据
    my.cnf
    Centos7通过yum安装jdk8
    maven添加本地包命令mvn install:install-file
    Mysql——查看数据库,表占用磁盘大小
  • 原文地址:https://www.cnblogs.com/wmm007/p/8000466.html
Copyright © 2011-2022 走看看