read a password protected xls file

John Machin sjmachin at lexicon.net
Tue Jan 13 01:55:21 CET 2009


On Jan 13, 3:35 am, thomas.steffe... at googlemail.com wrote:
> Hello,
> how can I read (and parse) a password protected xls file, perhaps with
> the package xlrd?
> Thanks for your hints, Thomas

Perhaps not with xlrd. google("xlrd"); *first* hit displayed:
"""
Lingfo - xlrd extension
11 Jun 2007 ... Exclusions: xlrd will not attempt to decode password-
protected (encrypted) files . Otherwise, it will safely and reliably
ignore any of these ...
www.lexicon.net/sjmachin/xlrd.htm - 15k - Cached - Similar pages -
"""

You'll need a Windows box with Excel installed.
Get the package described here: http://wiki.python.org/moin/Win32All
Install it, do the "makepy" thing [see the docs] to link with the
Excel COM routines.
Ask questions on the mailing list described on
http://mail.python.org/mailman/listinfo/python-win32

Sample code:

import sys
import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
print "Excel library version:", xlApp.Version
filename, password = sys.argv[1:3]
# filename should be absolute path e.g. r'C:\myfiles\foo.xls'
xlwb = xlApp.Workbooks.Open(filename, Password=password)
# xlwb = xlApp.Workbooks.Open(filename)
""" From the MS VBA Help for Excel 2003:
Password   Optional Variant. A string that contains the password
required to open a protected workbook. If this argument is omitted and
the workbook requires a password, the user is prompted for the
password.
"""
print repr(xlwb)
print xlwb.FullName
xlws = xlwb.Sheets(1) # counts from 1, not from 0
print repr(xlws)
print xlws.Name
print xlws.Cells(1, 1) # that's A1

HTH,
John




More information about the Python-list mailing list