Openpyxl password protect excel file python

0 votes

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, 2022 in Others by Kithuzzz
• 38,000 points
8,327 views

1 answer to this question.

0 votes

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

answered Sep 24, 2022 by narikkadan
• 63,600 points

Related Questions In Others

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, 2022 in Others by narikkadan
• 63,600 points
819 views
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, 2022 in Others by narikkadan
• 63,600 points
2,258 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, 2022 in Others by narikkadan
• 63,600 points
4,183 views
0 votes
1 answer
0 votes
1 answer

How to save a new sheet in an existing excel file, using Pandas?

import pandas as pd import numpy as np path ...READ MORE

answered Dec 10, 2022 in Others by narikkadan
• 63,600 points
7,600 views
0 votes
0 answers

How to save Excel sheets to PDF using Python

I have a multiple-sheet Excel spreadsheet. I ...READ MORE

Apr 10, 2023 in Others by Kithuzzz
• 38,000 points
6,122 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,020 points
1,122 views
0 votes
1 answer

Add a correct format date in Excel file with Python openpyxl

Use just datetime.fromisoformat(row['Date']) but I didn't have ...READ MORE

answered Feb 9, 2023 in Others by narikkadan
• 63,600 points
2,672 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, 2022 in Others by narikkadan
• 63,600 points
7,598 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