gpt4 book ai didi

Python xlsx 单元格保护(锁)

转载 作者:行者123 更新时间:2023-12-04 20:00:33 25 4
gpt4 key购买 nike

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

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

有人找到解决方法吗?

最佳答案

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()

protect()有关完整详细信息的文档部分。

Cell Protection Example

关于Python xlsx 单元格保护(锁),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15271779/

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