2

我需要导出一个列表以 Excel 与一些单元格或列保护(只读)。

问题是我使用 openpyxl python 模块写入 xlsx,但我认为只有 xlwt 具有单元保护功能。而且 xlwt 似乎不支持 xlsx。

有人找到解决方法吗?

4

1 回答 1

4

Python 模块XlsxWriter允许您编写 XLSX 文件并添加工作表单元格保护(除其他外):

from xlsxwriter.workbook import Workbook

workbook = Workbook('protection.xlsx')
worksheet = workbook.add_worksheet()

# Create a cell format with protection properties.
unlocked = workbook.add_format({'locked': False})

# Format the columns to make the text clearer.
worksheet.set_column('A:A', 40)

# Turn worksheet protection on.
worksheet.protect()

# Write a locked and unlocked cell.
worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')

worksheet.write_formula('B1', '=1+2')  # Locked by default.
worksheet.write_formula('B2', '=1+2', unlocked)

workbook.close()

有关完整详细信息,请参阅文档的保护()部分。

单元保护示例

于 2013-03-08T00:56:25.190 回答