Excel中使用python的块复制粘贴选项

问题描述 投票:0回答:1

是否可以将复制粘贴单元格选项锁定在使用python生成的Excel文件中?我已使用XLSXWriter生成excel文件。

python xlsxwriter
1个回答
0
投票

在Excel中,您可能会使用Worksheet Protect来防止将数据复制到工作表中。

您可以使用工作表protect()方法使用XlsxWriter进行此操作。这是一个例子:

import xlsxwriter

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

# Create some cell formats with protection properties.
unlocked = workbook.add_format({'locked': False})
hidden = workbook.add_format({'hidden': True})

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

# Turn worksheet protection on.
worksheet.protect()

# Write a locked, unlocked and hidden 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('A3', "Cell B3 is hidden. The formula isn't visible.")

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

workbook.close()

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.