Python 处理 Excel 表格,openpyxl常见用法整理

Python 处理 Excel 表格,openpyxl常见用法整理

在工作中处理Excel时,可用Python增加处理效率,此处介绍openpyxl库的常见用法:
安装:pip install openpyxl

# -*- coding = utf-8 -*-
# @Time : 2021/12/17 9:04
# @Author : 修理工小刘
# @File : openpyxl_demo.py
# @Software: PyCharm

import openpyxl

# 本例是基于openpyxl的操作excel文件demo
class MYOPENPY:
    def __init__(self):
        pass

    # 创建文件
    def createfile(self):
        self.wb = openpyxl.Workbook()

    # 加载文件
    def loadfile(self):
        self.wb = openpyxl.load_workbook("myexcel.xlsx")

    # 保存文件
    def savefile(self):
        self.wb.save("myexcel.xlsx")

    # 打印表单
    def printsheet(self):
        print(self.wb.sheetnames)

    # 创建表单
    def createsheet(self):
        self.ws = self.wb.create_sheet("mySheet", 0)

    # 获取表单
    def getsheet(self):
        # 方式1
        self.ws = self.wb["mySheet"]
        # 方式2 已经弃用了,不推荐
        # self.ws = self.wb.get_sheet_by_name("mySheet")

    # 获取单元格
    def getcell(self):
        # 方式1
        self.cell = self.ws["A1"]
        # 方式2
        self.cell = self.ws.cell("A1")
        # 方式3 row行 column列 行列都是从1开始,推荐这种方式
        self.cell = self.ws.cell(1, 1)

    # 填写单元格
    def writecell(self):
        # 方式1
        self.ws["A2"] = 10
        # 方式2
        self.ws.cell(2, 2, value=20)

    # 获取单元格索引
    def getcellidx(self):
        self.cell = self.ws["C6"]
        # 获取行标 6
        print(self.cell.row)
        # 获取列标 3 = C
        print(self.cell.column)
        # 获取列名 "C"
        print(self.cell.column_letter)
        # 获取坐标 "C6"
        print(self.cell.coordinate)
        # 获取数据类型 默认n数值,s字符串,d日期
        print(self.cell.data_type)
        # 单元格编码格式 utf-8
        print(self.cell.encoding)

    # 获取多个单元格并打印
    def getmorecell(self):
        self.cellrange = self.ws["A1":"B2"]
        # 是一个二维元组,元组和列表的区别在于不能修改
        print(self.cellrange)
        # 打印B2的值
        print(self.cellrange[1][1].value)

    # 获取单行或单列
    def getlist(self):
        # 获取列,保存为一维元祖 遍历即可
        self.column_A = self.ws["A"]
        # 获取行,保存为一维元祖 遍历即可
        self.row_1 = self.ws[1]
        # 获取多列,保存为二维元祖 遍历即可
        self.column_AB = self.ws["A":"B"]
        # 获取多行,保存为二维元祖 遍历即可
        self.row_1_6 = self.ws[1:6]

    # 行列遍历
    def ergodic(self):
        # 行遍历
        for i in self.ws.rows:
            print(i)
        # 行遍历 带范围
        for i in self.ws.iter_rows(min_row=1, max_row=3, min_col=1, max_col=4):
            print(i)
        # 列遍历
        for i in self.ws.columns:
            print(i)
        # 列遍历 带范围
        for i in self.ws.iter_cols(min_row=1, max_row=3, min_col=1, max_col=4):
            print(i)

    # 在尾部插入行
    def appendrow(self):
        self.ws.append([1, 2, 3])

    # 其他不常见操作
    def otherprocess(self):
        # 获取当前表单
        self.ws = self.wb.active
        # 表单改名
        self.ws.title = "yorSheet"
        # 遍历值
        for v in self.ws.values:
            print(v)
        # 删除行列,注意,会自动向前填充
        self.ws.delete_rows(3)
        self.ws.delete_cols(1)
        # 设置最大行,最大列
        self.ws.max_column = 10
        self.ws.max_row = 10

    # 参考教程
    def course(self):
        course1 = "https://www.imooc.com/article/291433"
        course2 = "https://blog.csdn.net/weixin_41546513/article/details/109555832"

    # 例程1 逐行遍历
    def demo1(self):
        wb = openpyxl.Workbook()
        ws = wb.active
        ws.title = "test"
        # 就是正常顺序
        for i in range(1, 4): # 表示 1 2 3
            for j in range(1, 5):
                ws.cell(row=i, column=j, value=i+j)
        # 正常顺序
        for row in ws:
            for cell in row:
                print(cell.value)

    # 例程2 逐列遍历
    def demo2(self):
        wb = openpyxl.Workbook()
        ws = wb.active
        # 数据写入
        for i in range(1, 4):
            for j in range(1, 5):
                ws.cell(row=i, column=j, value=str(i)+","+str(j))
        # 逐列遍历
        for col in ws.iter_cols(min_row=1, max_col=4, max_row=3):
            for cell in col:
                print(cell.value)

if __name__ == '__main__':
    print('begin')
    my = MYOPENPY()
    my.loadfile()
    my.getsheet()
    my.getcellidx()
    # my.savefile()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值