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.
Is openpyxl safe to use? The python package openpyxl was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.
Workbook is the top-level container for all document information. class openpyxl.workbook.workbook. Workbook (write_only=False, iso_dates=False)[source] Bases: object. Workbook is the container for all other parts of the document.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With