How to login and set password in Excel using Python?

Viewed 28

I'm working to automate company data to store in ms excel. I'm storing data using pandas dataframe.

df = pd.concat([df, pd.DataFrame.from_records([{ 'Numplate': Numplate,
                                                 'CNIC': CNIC, 
                                                 'Random':Random}])],ignore_index = True)
writer = pd.ExcelWriter('demo.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()

I lock ms excel using this code

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.DisplayAlerts = False
wb = excel.Workbooks.Open("C:\yolo4FYP\demo.xlsx")
wb.SaveAs("C:\yolo4FYP\demo.xlsx", 51, 'test')                                               
wb.Close() 
excel.Application.Quit()

Password is setting and there is no error but How to open protected file to append more info in demo.xlsx . I checked all stack solution but not working for my problem

1 Answers

Have you tried .Unprotect?

excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.DisplayAlerts = False
wb = excel.Workbooks.Open("C:\yolo4FYP\demo.xlsx")
wb.Unprotect(password)
wb.SaveAs("C:\yolo4FYP\demo.xlsx", 51, 'test')                                               
wb.Close() 
excel.Application.Quit()
Related