Python xlwt - 只读列(单元格保护) [英] Python xlwt - making a column readonly (cell protect)

查看:1544
本文介绍了Python xlwt - 只读列(单元格保护)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道有一个cell_overwrite_ok标志不允许覆盖细胞的内容(所有细胞),但可以按细胞基础进行。



谢谢,
Sun

解决方案

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



因此,您的问题不是最好的框架, make cell read-only 。相反,问题是在保护工作表之后如何使单元格可以编辑。



...你在这里:

 从xlwt import工作簿,工作表,easyxf 

#...

#保护工作表 - 所有单元格将默认为只读
my_worksheet.protect = True#默认为False
my_worksheet.password =something_difficult_to_guess

#为只读只创建单元格样式可编辑单元格
editable = easyxf(protection:cell_locked false;)
read_only = easyxf()#cell_locked true是默认的

#应用您的新样式当写单元格
my_worksheet.write(0,0,不能触摸这个!,read_only)
my_worksheet.write(2,2,擦除我:),可编辑)

#...

单元格样式( easyxf class)也可用于声明背景颜色,字体重量等。



干杯。


Is there a way to make a particular cell read-only/write protected in python xlwt?

I know there's is a cell_overwrite_ok flag which does not allow to overwrite contents of cells (all cells) but can this be done on cell by cell basis.

Thanks, Sun

解决方案

Excel cells have a locked attribute that is enabled by default. However, this attribute is only invoked when the worksheet's protection attribute is also set to True. If the worksheet is not protected, the locked attribute is ignored.

Therefore, your question isn't best framed as how to make cells read-only. Rather, the question is how to make cells editable after protecting the worksheet.

...Here you are:

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)

# ...

The cell styles (easyxf class) are also useful for declaring background color, font weight, etc.

Cheers.

这篇关于Python xlwt - 只读列(单元格保护)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆