Home > other >  Win32com. The problem of client to clear EXCEL file to open the password
Win32com. The problem of client to clear EXCEL file to open the password

Time:11-21

Early development using low version of OFFICE, use win32com. Client calls to EXCEL application, SaveAs method is used to empty file to open the password, all normal,
After the upgrade OFFICE, use the SaveAs method can change the password, but the password is set to null, not empty password, code is as follows:
Def remove_xlsx_password (filename, pw_str) :
XCL=win32com. Client. Dispatch (" Excel. Application ")
XCL. Visible=0 # application does not show
Try:
Wb=XCL. Workbooks. Open (filename, UpdateLinks=False, ReadOnly=False, Format=None, Password=pw_str)
Except:
Print (" ERROR: Remove XLSX file password [% s] "% (filename))
The else:
# XCL. DisplayAlerts=False message does not display the
If the wb!=None:
Wb. SaveAs (filename, None, Password=', WriteResPassword=' ')
Finally:
XCL. Quit ()

After using the Password attribute to resolve the problem, the code is as follows:

Def remove_xlsx_password (filename, pw_str) :
XCL=win32com. Client. Dispatch (" Excel. Application ")
XCL. Visible=0 # application does not show
Try:
Wb=XCL. Workbooks. Open (filename, UpdateLinks=False, ReadOnly=False, Format=None, Password=pw_str)
Except:
Print (" ERROR: Remove XLSX file password [% s] "% (filename))
The else:
# XCL. DisplayAlerts=False message does not display the
If the wb!=None:
Wb. Password='# set workbook Password empty
Wb. The Save ()
Wb. Close ()
# wb. SaveAs (filename, None, Password=', WriteResPassword=' ')
# above statement can be used for low version of the Office, but for the high version of the Office there will be a file open password can be modified, but not empty,
Finally:
XCL. Quit ()
  • Related