Python办公软件自动化,5分钟掌握openpyxl“骚”操作

2022-08-2714:40:51后端程序开发Comments1,256 views字数 4465阅读模式

各种数据需要导入Excel?多个Excel要合并?目前,Python处理Excel文件有很多库,openpyxl算是其中功能和性能做的比较好的一个。接下来我将为大家介绍各种Excel操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

打开Excel文件

新建一个Excel文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> from openpyxl import Workbook
    >>> wb = Workbook()

打开现有Excel文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> from openpyxl import load_workbook
    >>> wb2 = load_workbook('test.xlsx')

打开大文件时,根据需求使用只读或只写模式减少内存消耗。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

wb = load_workbook(filename='large_file.xlsx', read_only=True)

wb = Workbook(write_only=True)
获取、创建工作表

获取当前活动工作表:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> ws = wb.active

创建新的工作表:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> ws1 = wb.create_sheet("Mysheet") # insert at the end (default)
    # or
    >>> ws2 = wb.create_sheet("Mysheet", 0) # insert at first position
    # or
    >>> ws3 = wb.create_sheet("Mysheet", -1) # insert at the penultimate position

使用工作表名字获取工作表:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> ws3 = wb["New Title"]

获取所有的工作表名称:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> print(wb.sheetnames)
['Sheet2', 'New Title', 'Sheet1']
使用for循环遍历所有的工作表:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

>>> for sheet in wb:
...     print(sheet.title)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

保存

保存到流中在网络中使用:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> from tempfile import NamedTemporaryFile
    >>> from openpyxl import Workbook
    >>> wb = Workbook()
    >>> with NamedTemporaryFile() as tmp:
            wb.save(tmp.name)
            tmp.seek(0)
            stream = tmp.read()
保存到文件:

    >>> wb = Workbook()
    >>> wb.save('balances.xlsx')
保存为模板:

    >>> wb = load_workbook('document.xlsx')
    >>> wb.template = True
    >>> wb.save('document_template.xltx')
单元格

单元格位置作为工作表的键直接读取:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> c = ws['A4']

为单元格赋值:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> ws['A4'] = 4
    >>> c.value = 'hello, world'

多个单元格 可以使用切片访问单元格区域:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> cell_range = ws['A1':'C2']

使用数值格式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> # set date using a Python datetime
    >>> ws['A1'] = datetime.datetime(2010, 7, 21)
    >>>
    >>> ws['A1'].number_format
    'yyyy-mm-dd h:mm:ss'

使用公式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> # add a simple formula
    >>> ws["A1"] = "=SUM(1, 1)"

合并单元格时,除左上角单元格外,所有单元格都将从工作表中删除:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> ws.merge_cells('A2:D2')
    >>> ws.unmerge_cells('A2:D2')
    >>>
    >>> # or equivalently
    >>> ws.merge_cells(start_row=2, start_column=1, end_row=4, end_column=4)
    >>> ws.unmerge_cells(start_row=2, start_column=1, end_row=4, end_column=4) 
行、列

可以单独指定行、列、或者行列的范围:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> colC = ws['C']
    >>> col_range = ws['C:D']
    >>> row10 = ws[10]
    >>> row_range = ws[5:10]

可以使用Worksheet.iter_rows()方法遍历行:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> for row in ws.iter_rows(min_row=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>

同样的Worksheet.iter_cols()方法将遍历列:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> for col in ws.iter_cols(min_row=1, max_col=3, max_row=2):
    ...     for cell in col:
    ...         print(cell)
    <Cell Sheet1.A1>
    <Cell Sheet1.A2>
    <Cell Sheet1.B1>
    <Cell Sheet1.B2>
    <Cell Sheet1.C1>
    <Cell Sheet1.C2>

遍历文件的所有行或列,可以使用Worksheet.rows属性:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> ws = wb.active
>>> ws['C9'] = 'hello world'
>>> tuple(ws.rows)
((<Cell Sheet.A1>, <Cell Sheet.B1>, <Cell Sheet.C1>),
(<Cell Sheet.A2>, <Cell Sheet.B2>, <Cell Sheet.C2>),
(<Cell Sheet.A3>, <Cell Sheet.B3>, <Cell Sheet.C3>),
(<Cell Sheet.A4>, <Cell Sheet.B4>, <Cell Sheet.C4>),
(<Cell Sheet.A5>, <Cell Sheet.B5>, <Cell Sheet.C5>),
(<Cell Sheet.A6>, <Cell Sheet.B6>, <Cell Sheet.C6>),
(<Cell Sheet.A7>, <Cell Sheet.B7>, <Cell Sheet.C7>),
(<Cell Sheet.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>),
(<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

Worksheet.columns属性:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> tuple(ws.columns)
((<Cell Sheet.A1>,
<Cell Sheet.A2>,
<Cell Sheet.A3>,
<Cell Sheet.A4>,
<Cell Sheet.A5>,
<Cell Sheet.A6>,
...
<Cell Sheet.B7>,
<Cell Sheet.B8>,
<Cell Sheet.B9>),
(<Cell Sheet.C1>,
<Cell Sheet.C2>,
<Cell Sheet.C3>,
<Cell Sheet.C4>,
<Cell Sheet.C5>,
<Cell Sheet.C6>,
<Cell Sheet.C7>,
<Cell Sheet.C8>,
<Cell Sheet.C9>))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

使用Worksheet.append()或者迭代使用Worksheet.cell()新增一行数据:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> for row in range(1, 40):
    ...     ws1.append(range(600))

    >>> for row in range(10, 20):
    ...     for col in range(27, 54):
    ...         _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))

插入操作比较麻烦。可以使用Worksheet.insert_rows()插入一行或几行:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

     >>> from openpyxl.utils import get_column_letter
     >>> ws.insert_rows(7) 
     >>> row7 = ws[7]
     >>> for col in range(27, 54):
    ...         _ = ws3.cell(column=col, row=7, value="{0}".format(get_column_letter(col)))

Worksheet.insert_cols()操作类似。Worksheet.delete_rows()Worksheet.delete_cols()用来批量删除行和列。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

只读取值

使用Worksheet.values属性遍历工作表中的所有行,但只返回单元格值:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    for row in ws.values:
       for value in row:
         print(value)

Worksheet.iter_rows()Worksheet.iter_cols()可以设置values_only参数来仅返回单元格的值:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html

    >>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2, values_only=True):
    ...   print(row)
    (NoneNoneNone)
    (NoneNoneNone)
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/27481.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/27481.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定