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!

lokheart
  • 23,743
  • 39
  • 98
  • 169

2 Answers2

2

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

Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • I'm not sure, I just receive a prompt when I try to open the xls file, how can I know it? Thanks! – lokheart Sep 24 '10 at 03:32
  • @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 Sep 24 '10 at 03:34
  • 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 Sep 24 '10 at 03:42
  • @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 Sep 24 '10 at 04:14
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.

CodeTalker
  • 1,683
  • 2
  • 21
  • 31