I'm reading data from Excel with openpyxl library but I always have to remove password and then save file to load it on my Python project. All of this is working fine!
But I'd like to automate this process and not to have to manually remove the password and then save the file. I searched on the web something to automatically do this but the code is not working properly:
import openpyxl
import win32com.client
def unprotect_xlsm(filename):
xcl = win32com.client.Dispatch('Excel.Application')
pw_str = 'mypassword'
wb = xcl.workbooks.open(filename)
wb.Unprotect(pw_str)
wb.UnprotectSharing(pw_str)
xcl.DisplayAlerts = False
wb.Save()
xcl.Quit()
unprotect_xlsm("SAE/SAE_R360.xlsm")
wb = openpyxl.load_workbook("SAE/SAE_R360.xlsm", data_only=True)
This file is a .xlsm Excel extension and the console is printing the following error:
File "<COMObject <unknown>>", line 2, in open
pywintypes.com_error: (-2147352567, 'Exceção.', (0, 'Microsoft Excel', 'Não foi possível
encontrar SAE/teste.xlsx. É possível que ele tenha sido movido, renomeado ou excluído?',
'xlmain11.chm', 0, -2146827284), None)
The msg says it was not possible to find the file. It may be moved, renamed or trashed.
Any help, pls?