Openpyxl password protect excel file python

0 votes

I've already linked to these posts here - herehere, and here. Don't mark it as duplicate, please.

I'm attempting to use my excel to perform some procedures before saving and password-protecting them. I'm using openpyxl 3.0.9.

So, I tried the below

for search, v in merge_df.groupby(['Country']):
    writer = pd.ExcelWriter(f"BC_{Country}.xlsx", engine='xlsxwriter')
    v.to_excel(writer,columns=col_list,sheet_name=f'BC_{Country}',index=False, startrow = 1)
    wb1 = load_workbook(filename = f"BC_{Country}.xlsx")
    sheet_to = wb1.worksheets[0]
    wb1.security.workbookPassword = "test"
    wb1.save(f"BC_{Country}.xlsx")

But I get the below error:

AttributeError: 'NoneType' object has no attribute 'workbookPassword'

How can I protect my excel sheet with a password?

So, the user can only open it with a password.

Sep 23 in Others by Kithuzzz
• 12,240 points
30 views

1 answer to this question.

0 votes

Please refer to the documentation here. Workbooks can be secured against certain actions, such as "...viewing secret worksheets, adding, moving, deleting, or hiding worksheets, and renaming worksheets."

The users should be able to read and see the file, but not edit or overwrite the data, in my opinion. Therefore, you can lock it using worksheet protection. The code below will create a new file, allow you to type something, then lock it with a password and save it. The user must enter the password in the Excel file's Review tab > Unprotect Sheet to unlock and modify it. I hope it works.

wb=openpyxl.Workbook()
sheet1=wb.active
sheet1.cell(3,3).value = "Though shalt not overwrite"
sheet1.protection.sheet = True
sheet1.protection.password = 'test'
wb.save('Book1.xlsx')

Open excel error when you try to double click/type something

enter image description here

answered Sep 24 by narikkadan
• 20,880 points

Related Questions In Others

0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
4,889 views
0 votes
1 answer

Convert JSON file to Excel File

Hi To convert a file, Set options and ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
200 views
0 votes
1 answer

How to import excel file in Oracle SQL live

Hello, there are a few steps You'll ...READ MORE

answered Feb 18 in Others by gaurav
• 18,960 points
396 views
0 votes
1 answer

Removing specific rows in an Excel file using Azure Data Factory

Under the 'Source' tab, choose the number ...READ MORE

answered Sep 23 in Others by narikkadan
• 20,880 points
20 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,080 points
342 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,017 views
0 votes
1 answer

Reading/parsing Excel (xls) files with Python

I highly recommend xlrd for reading .xls files. But there are ...READ MORE

answered Sep 21 in Others by narikkadan
• 20,880 points
17 views
0 votes
1 answer

Excel VBA if file closed, then open and paste, else just paste data

Slightly re-worked to add full workbook/sheet qualifiers ...READ MORE

answered Sep 21 in Others by narikkadan
• 20,880 points
14 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP