【问题标题】:Python xlwt - making a column readonly (cell protect)Python xlwt - 使列只读(单元格保护)
【发布时间】:2012-01-21 12:21:57
【问题描述】:

有没有办法在 python xlwt 中使特定单元格只读/写保护?

我知道有一个 cell_overwrite_ok 标志,它不允许覆盖单元格(所有单元格)的内容,但可以逐个单元格地完成。

谢谢, 孙

【问题讨论】:

    标签: python excel overriding protected xlwt


    【解决方案1】:

    Excel 单元格有一个默认启用的 locked 属性。但是,仅当工作表的 protection 属性也设置为 True 时,才会调用此属性。如果工作表不受保护,则忽略 locked 属性。

    因此,最好不要将您的问题描述为如何使单元格成为只读。相反,问题是如何在保护工作表后使单元格可编辑

    ...你在这里:

    from xlwt import Workbook, Worksheet, easyxf
    
    # ...
    
    # Protect worksheet - all cells will be read-only by default
    my_worksheet.protect = True  # defaults to False
    my_worksheet.password = "something_difficult_to_guess"
    
    # Create cell styles for both read-only and editable cells
    editable = easyxf("protection: cell_locked false;")
    read_only = easyxf("")  # "cell_locked true" is default
    
    # Apply your new styles when writing cells
    my_worksheet.write(0, 0, "Can't touch this!", read_only)
    my_worksheet.write(2, 2, "Erase me :)", editable)
    
    # ...
    

    单元格样式(easyxf 类)也可用于声明背景颜色、字体粗细等。

    干杯。

    【讨论】:

    • 如果我必须用密码保护整个 excel 文件怎么办?
    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 2011-01-30
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多