xlsxwriter 设置密码打开文档?
xlsxwriter set password to open document?
我正在尝试使用 xlsxwriter 创建一个带有工作簿密码的 xlsx 文件。我浏览了文档并浏览了源代码,但似乎找不到任何相关信息。
您是否尝试这样设置密码:
wb = Workbook()
ws = wb.worksheets[0]
ws.protect('abc123')
根据文档here
Worksheet level passwords in Excel offer very weak protection. They do
not encrypt your data and are very easy to deactivate. Full workbook
encryption is not supported by XlsxWriter since it requires a
completely different file format and would take several man months to
implement.
可以给工作表加密码,但不是强保护的方法
worksheet.protect(<password>)
worksheet.protect() 仅保护工作表不被编辑。
但是,我们可以将 protect() 方法与隐藏格式相结合,以在第一眼看到时隐藏数据。只有知道密码才能解锁和取消隐藏单元格。
这是我的方法:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
test_content = (
['Hi', 123],
['I', 1123],
['AM', 1231231],
['LOCKED',1123123],
)
row = 0
col = 0
for text, num in (test_content):
worksheet.write(row, col, text)
worksheet.write(row, col + 1, num)
worksheet.set_row(row, None, None, {'hidden': True}) #hide the row
row += 1
worksheet.protect('password') # set the password and protect
workbook.close()
找到问题的完美解决方案。不完全使用 xlsxwriter 但工作完美。
使用 pywin32,遗憾的是仅限于 windows 台机器。
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
#Before saving the file set DisplayAlerts to False to suppress the warning dialog:
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(your_file_name)
# refer https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb214129(v=office.12)?redirectedfrom=MSDN
# FileFormat = 51 is for .xlsx extension
wb.SaveAs(your_file_name, 51, 'your password')
wb.Close()
excel.Application.Quit()
这是对原始答案的 link。
我正在尝试使用 xlsxwriter 创建一个带有工作簿密码的 xlsx 文件。我浏览了文档并浏览了源代码,但似乎找不到任何相关信息。
您是否尝试这样设置密码:
wb = Workbook()
ws = wb.worksheets[0]
ws.protect('abc123')
根据文档here
Worksheet level passwords in Excel offer very weak protection. They do not encrypt your data and are very easy to deactivate. Full workbook encryption is not supported by XlsxWriter since it requires a completely different file format and would take several man months to implement.
可以给工作表加密码,但不是强保护的方法
worksheet.protect(<password>)
worksheet.protect() 仅保护工作表不被编辑。 但是,我们可以将 protect() 方法与隐藏格式相结合,以在第一眼看到时隐藏数据。只有知道密码才能解锁和取消隐藏单元格。
这是我的方法:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
test_content = (
['Hi', 123],
['I', 1123],
['AM', 1231231],
['LOCKED',1123123],
)
row = 0
col = 0
for text, num in (test_content):
worksheet.write(row, col, text)
worksheet.write(row, col + 1, num)
worksheet.set_row(row, None, None, {'hidden': True}) #hide the row
row += 1
worksheet.protect('password') # set the password and protect
workbook.close()
找到问题的完美解决方案。不完全使用 xlsxwriter 但工作完美。 使用 pywin32,遗憾的是仅限于 windows 台机器。
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
#Before saving the file set DisplayAlerts to False to suppress the warning dialog:
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(your_file_name)
# refer https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb214129(v=office.12)?redirectedfrom=MSDN
# FileFormat = 51 is for .xlsx extension
wb.SaveAs(your_file_name, 51, 'your password')
wb.Close()
excel.Application.Quit()
这是对原始答案的 link。