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
• 20,660 points
521 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
• 37,660 points

Related Questions In Others

0 votes
1 answer

Calculating the mean and std on excel file using python

Use pandas to do this: import pandas as pd df = ...READ MORE

answered Nov 4 in Others by narikkadan
• 37,660 points
54 views
0 votes
1 answer

How to convert pdf file to excel file using python

Just specify your whole output path instead ...READ MORE

answered Nov 4 in Others by narikkadan
• 37,660 points
124 views
0 votes
1 answer
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,100 points
394 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,120 views
0 votes
1 answer

Export a simple Dictionary into Excel file in python

You can use pandas. import pandas as pd dict1 ...READ MORE

answered Oct 23 in Others by narikkadan
• 37,660 points
83 views
0 votes
1 answer

Password Protecting an Excel file in C#

Try this: using Microsoft.Office.Interop.Excel //create your spreadsheet here... WorkbookObject.Password = ...READ MORE

answered Oct 30 in Others by narikkadan
• 37,660 points
44 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