how to read password protected excel in python

自作多情 提交于 2019-12-11 07:30:24

问题


I'm new to python programming, and I am trying to read a password protected file using python, the code is shown below:

import sys
import win32com.client

xlApp = win32com.client.Dispatch("Excel.Application")
print "Excel library version:", xlApp.Version
filename,password = 'C:\myfiles\foo.xls', 'qwerty12'
xlwb = xlApp.Workbooks.Open(filename, Password=password)

But then the xls file is loaded but still prompt me to provide the password, I can't let python to enter the password for me.

What have I done wrong? Thanks!


回答1:


Open takes two types of password, namely:

Password: password required to open a protected workbook.
WriteResPassword : password required to write to a write-reserved workbook

So in your case , is it write protected or protection on open?

Also there is a discussion on SO that says that this does not work with named parameters, So try providing all parameter values with the defaults

  • How to open write reserved excel file in python with win32com?

Default values are documented in MSDN

  • http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open.aspx



回答2:


Use this to open password protected file

xlwb = xlApp.Workbooks.Open(filename, False, True, None, password)

I hope this works. It worked for me.



来源:https://stackoverflow.com/questions/3783980/how-to-read-password-protected-excel-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!