问题
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