openpyxl各种操作汇总 —— 读写单元格、行、列

本文展示了如何使用Python的openpyxl库对Excel工作表的单元格进行读写、赋值、格式化、公式计算、行列操作以及插入和删除。通过iter_rows和iter_cols方法可以便捷地遍历工作表内容,append和insert_rows等函数用于增删数据。
摘要由CSDN通过智能技术生成

单元格

单元格位置作为工作表的键直接读取:

    >>> c = ws['A4']

为单元格赋值:

    >>> ws['A4'] = 4
    >>> c.value = 'hello, world'
    >>> _ = ws.cell(column=1, row=2, value="AA")

多个单元格

可以使用切片访问单元格区域:

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

使用数值格式:

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

使用公式:

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

合并单元格时,除左上角单元格外,所有单元格都将从工作表中删除:

    >>> 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) 

行、列

可以单独指定行、列、或者行列的范围:

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

可以使用Worksheet.iter_rows()方法遍历行:

    >>> 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()方法将遍历列:

    >>> 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属性:

    >>> 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>))

Worksheet.columns属性:

    >>> 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>))

使用Worksheet.append()或者迭代使用Worksheet.cell()新增一行数据:

    >>> 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()插入一行或几行:

     >>> 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()用来批量删除行和列。

只读取值

使用Worksheet.values属性遍历工作表中的所有行列,但只返回单元格值:

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

Worksheet.iter_rows()Worksheet.iter_cols()可以设置values_only参数来仅返回单元格的值:

    >>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2, values_only=True):
    ...   print(row)
    (None, None, None)
    (None, None, None)


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值