Unprotect an Excel file programmatically

0 votes

A client has sent us an Excel document with open protection and Write Reserve enabled. In order to use the Python xlrd module to open the Excel file, I need to remove the protection. My software can open the Excel file after installing the pywin32 package and providing the two passwords. It can also save and close the file without making any mistakes. The Unprotect commands that I'm using don't fail, but they also don't remove the protection, as mentioned in the MSDN Network. After my software is finished, the saved file still needs two passwords to open it. Here is what I currently have:

import os, sys
impdir = "\\\\xxx.x.xx.x\\allshare\\IT\\NewBusiness\\Python_Dev\\import\\"
sys.path.append(impdir)
from UsefulFunctions import *
import win32com.client

wkgdir = pjoin(nbShare, 'NorthLake\\_testing')
filename = getFilename(wkgdir, '*Collections*.xls*')
xcl = win32com.client.Dispatch('Excel.Application')
xcl.visible = True
pw_str = raw_input("Enter password: ")
try:
    wb = xcl.workbooks.open(filename, 0, False, None, pw_str, pw_str)
except Exception as e:
    print "Error:", str(e)
    sys.exit()
wb.Unprotect(pw_str)
wb.UnprotectSharing(pw_str)
wb.Save()
xcl.Quit()

Can anyone provide me the correct syntax for unprotect commands that will work?

Dec 10, 2022 in Others by Kithuzzz
• 28,700 points
97 views

1 answer to this question.

0 votes

Try this. It worked for me. 

def Remove_password_xlsx(filename, pw_str):
    xcl = win32com.client.Dispatch("Excel.Application")
    wb = xcl.Workbooks.Open(filename, False, False, None, pw_str)
    xcl.DisplayAlerts = False
    wb.SaveAs(filename, None, '', '')
    xcl.Quit()
answered Dec 11, 2022 by narikkadan
• 53,160 points

Related Questions In Others

0 votes
1 answer

How to update an existing excel .ods file?

It cannot be altered unless the original ...READ MORE

answered Oct 8, 2022 in Others by narikkadan
• 53,160 points
254 views
0 votes
1 answer

Create Graph from data in an excel file

Your first step would be to become ...READ MORE

answered Oct 17, 2022 in Others by narikkadan
• 53,160 points
98 views
0 votes
1 answer
0 votes
1 answer

How to find out how many rows and columns to read from an Excel file with PHPExcel?

Solution: $file_name = htmlentities($_POST['file_name']); $sheet_name = htmlentities($_POST['sheet_name']); $number_of_columns = htmlentities($_POST['number_of_columns']); $number_of_rows ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 53,160 points
1,466 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,323 views
0 votes
1 answer
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, 2022 in Others by narikkadan
• 53,160 points
581 views
0 votes
1 answer
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