Protecting Workbook in openpyxl

点点圈 提交于 2020-03-13 05:16:01

问题


I try to protect an Excel workbook with openpyxl.

So far I had a look into the different, potentially relevant classes but I can't find a set_password method like the one for worksheets.

There happens to be the workbook.protection module that I tried my luck with. My code, boiled down to the absolute relevant minimum is as follows:

from openpyxl import Workbook
from openpyxl.workbook.protection import WorkbookProtection

wb = Workbook()
wb.security = WorkbookProtection(workbookPassword='0000', revisionsPassword = '0000', lockWindows = True, lockStructure = True, lockRevision = True)
wb.create_sheet("testSheet")
wb.save("test.xlsx")

I don't get any errors but no protection either. Any help would be dearly appreciated.


回答1:


This doesn't currently work in Openpyxl (v2.4.8) - the password is not written to the output file. There is an open PR for this (https://bitbucket.org/openpyxl/openpyxl/pull-requests/180/read-and-write-workbook-protection/diff), so hopefully it will be included in future releases.



来源:https://stackoverflow.com/questions/45220389/protecting-workbook-in-openpyxl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!