views:

86

answers:

1

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  A: 

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

Default values are documented in MSDN

pyfunc
I'm not sure, I just receive a prompt when I try to open the xls file, how can I know it? Thanks!
lokheart
@lokheart: if you get the prompt to open files and do not pass in the password and it opens excel file in read only mode then it is only write protected. See my edited response, it looks like named parameters do not work. Let me know, if answer helps you.
pyfunc
I fixed it using: xlwb = xlApp.Workbooks.Open(filename, 0, True, None, password)But I don't quite understand the 2nd parameters of Workbooks.Open, what is it? Thanks!
lokheart
@kokheart: If Microsoft Excel is opening a file in the WKS, WK1, or WK3 format and the UpdateLinks argument is 2, Microsoft Excel generates charts from the graphs attached to the file. If the argument is 0, no charts are created.
pyfunc