有没有办法在 python xlwt 中使特定单元格只读/写保护?
我知道有一个 cell_overwrite_ok 标志,它不允许覆盖单元格(所有单元格)的内容,但这可以逐个单元格地完成。
谢谢,
太阳
Excel 单元格有一个locked默认情况下启用的属性。但是,仅当工作表的保护属性也设置为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
class) 对于声明背景颜色、字体粗细等也很有用。
Cheers.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)