zoukankan      html  css  js  c++  java
  • openpyxl模块处理excel文件

    python模块之——openpyxl 处理xlsx/ xlsm文件

    项目原因需要编辑excel文件,经过查询,最先尝试xlwt 、wlrd这个两个模块,但是很快发现这两个模块只能编辑xls文件,然而office07 之后的excel默认保存格式都是xlsx / xlsm,所以xlwt 、wlrd解决不了问题。后来发现openpyxl模块就是为处理 xlsx/xlsm/xltx/xltm而存在的,最终用该模块完美的解决的问题。但是请注意,openpyxl也只能处理xlsx、xlsm文件,对xls也只能改道之前提到的两个模块。

    一、openpyxl模块介绍

    Author: Eric Gazoni , Charlie Clark

    网址:openpyxl.readthedocs.io/en/stable/

    该模块有几个重要的概念:Workbooks、Sheets、Cells,通过这三个概念,就可以定位到最细的cell,并对其进行删除、修改、增加等操作。

    二、安装

    在cmd中 $ pip install openpyxl就可以完成

    三、使用

    1、创建新文件并编辑

    import openpyxl
    ################
    #创建一个新的workbook
    wb=openpyxl.Workbook()
    #创建一个worksheet
    ws = wb.active
    #插入sheet
    ws1=wb.create_sheet("Mysheet") # insert at the end (default)
    ws2 = wb.create_sheet("Mysheet", 0) # insert at first position
    #对sheet2名进行修改
    ws2.title = "New Title"  
    #获取一个sheet,使其可进行编辑,使用方式和字典使用一样
    w2=wb["New Tile"] #这样就可以对sheet3编辑了
    #改变sheet名的背景色
    ws2.sheet_properties.tabColor = "1072BA"
    #获取所有已经创建的sheetnames
    wb.sheetnames
    #遍历所有的sheet
    for sheet in wb:
        print(sheet.title)
    ##########################
    #编辑处理cell的内容 (方法一)
    ws1["A4"]="Hello"
    #编辑处理cell的内容 (方法二)
    ws1.cell(row=4,column=2,value=10)
    #编辑处理cell的内容 (方法三)
    ws1.cell(row=4,column=3).value=20
    #Notes & Warning:
    #When a worksheet is created in memory, it contains no cells. They are created when first accessed.
    #When a worksheet is created in memory, it contains no cells. They are created when first accessed.
    #Because of this feature, scrolling through cells instead of accessing them directly will create them all in memory, even if you don’t assign them a value.
    
    #Something like
     for i in range(1,101):
            for j in range(1,101):
                  ws.cell(row=i, column=j)
    #will create 100x100 cells in memory, for nothing.
    ##########################
    #处理多cell
    cell_range=ws["A1":"C2"]
    colC=ws["C"]
    col_range=ws["C:D"]
    row10=ws[10]
    row_range=ws[3:6]
    #遍历cell (按照rows遍历)
    for row  in ws.iter_rows(min_row=1,min_col=1,max_col=3,max_row=2):
    ````for cell in row:
    ````    print(cell)
    <Cell Sheet1.A1>
    <Cell Sheet1.B1>
    <Cell Sheet1.C1>
    <Cell Sheet1.A2>
    <Cell Sheet1.B2>
    <Cell Sheet1.C2>
    #遍历cell (按照cols遍历)
    for row  in ws.iter_cols(min_row=1,min_col=1,max_col=3,max_row=2):
    ````for cell in row:
    ````    print(cell)
    <Cell Sheet1.A1>
    <Cell Sheet1.A2>
    <Cell Sheet1.B1>
    <Cell Sheet1.B2>
    <Cell Sheet1.C1>
    <Cell Sheet1.C2>
    
    wb.save("G:/TEMP/test.xlsx")

    2.读取并编辑已经存在的文件

    import openpyxl
    FilePath="G:/TEMP"
    FileName="test.xlsx"
    wb=openpyxl.load_workbook(FilePath+"/"+FileName)
    lisSheetName=wb.sheetnames
    print(lisSheetName)
    ws=wb[lisSheetName[1]]
    #编辑处理cell的内容 (方法一)
    ws["A4"]="Hello"
    #编辑处理cell的内容 (方法二)
    ws.cell(row=4,column=2,value=10)
    #编辑处理cell的内容 (方法三)
    ws.cell(row=4,column=3).value=20
    wb.save(FilePath+"/"+FileName)

    3. 附加

    #在excel插入函数和公式
    ws["D4"]="=SUM(1,1)"
    ws["E4"]="=SUM(A4:D4)"
    # 插入画图
    #(略)

    更详细可搜索openpyxl进入官方文档查询

  • 相关阅读:
    比较器
    堆排序
    快速排序
    荷兰国旗问题
    python查漏补缺 --- 基础概念及控制结构
    redis 交集、并集、差集
    spark问题
    IDEA写scala简单操作
    hive 中遇到的正则
    scala模式匹配
  • 原文地址:https://www.cnblogs.com/zdwu/p/9262016.html
Copyright © 2011-2022 走看看