Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protecting Workbook in openpyxl

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.

like image 609
Thomas Avatar asked Jul 20 '17 16:07

Thomas


People also ask

Is openpyxl secure?

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.

What is workbook in openpyxl?

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.


1 Answers

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.

like image 100
gar Avatar answered Oct 08 '22 17:10

gar