2

I am trying to set password for excel file generated using openpyxl. I went through openpyxl. Openpyxl has a workbook protection functionality, but it does not support whole workbook lock using password as it does in MS Excel. I also looked into xlsxwriter, xlrd, xlwt.

Is there any way in python which i can set password for an excel file ?

Thanks in advance.

Sanjo
  • 21
  • 1
  • 1
  • 4
  • Try here.. Think this answers your question. [link](https://stackoverflow.com/questions/36122496/password-protecting-excel-file-using-python) – AmyOakes Dec 21 '17 at 08:57
  • 1
    I have seen this link, Openpyxl has no functionality to password protect a workbook. but that question does not provide a clear answer whether any python library other than Openpyxl can be used to lock an excel file or whole excel workbook. – Sanjo Dec 21 '17 at 09:37
  • it kind of does, basically i get the impression that you cant do it, check out this one [link](https://stackoverflow.com/questions/45220389/protecting-workbook-in-openpyxl?noredirect=1&lq=1) If you follow @gar link to the bitbucket page you mind find some more help there. – AmyOakes Dec 21 '17 at 11:39
  • @amyoakes - That did get implemented, but it still doesn't provide the functionality that I think Sanjo meant, which is to make it so that the workbook *cannot even be opened* without the password. The feature that got implemented was protection of the workbook *structure*, which simply means you cannot add, move, delete, hide, or unhide sheets (without disabling the protection, which is rather easy to do). You can still open the workbook normally. – John Y May 10 '18 at 23:11
  • See [my answer](https://stackoverflow.com/a/50282875/95852) on a similar (or perhaps duplicate) question. – John Y May 10 '18 at 23:35

1 Answers1

1

Can you try using the below code,

from openpyxl import Workbook
wb = Workbook()

ws=wb.worksheets[0]
ws.protection.set_password('test')
wb.save('sample.xlsx')
sujit
  • 185
  • 1
  • 2
  • 9