gpt4 book ai didi

Python xlwt - 使列只读(单元格保护)

转载 作者:太空狗 更新时间:2023-10-30 00:46:29 25 4
gpt4 key购买 nike

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

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

谢谢,太阳

最佳答案

Excel 单元格具有默认启用的锁定 属性。但是,仅当工作表的 protection 属性也设置为 True 时才会调用此属性。如果工作表不 protected ,则忽略 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 类)也可用于声明背景颜色、字体粗细等。

干杯。

关于Python xlwt - 使列只读(单元格保护),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com