5

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

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

谢谢,孙

4

1 回答 1

9

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

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

...给你:

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类)也可用于声明背景颜色、字体粗细等。

干杯。

于 2012-11-10T04:37:27.927 回答