8

The problem is pretty simple. Every week I receive a bunch of password protected excel files. I have to parse through them and write certain parts to a new file using Python. I am given the password for the files.

This was simple to handle when this was being done on Windows and I could just import win32com and use client.Dispatch. But we are now moving all our code to linux so no more win32com for me.

Is there a way to open and read data from a password protected excel sheet in python on linux?

I have been searching for simple way to open a password protected excel file but no luck. I also tried finding a way to just remove the password protection so I can use xlrd like I would on a file that is not password protected but no luck going that route either.

Any help would be most appreciated.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jacobr365
  • 846
  • 11
  • 24

1 Answers1

6

with libreoffice and unoconv

unoconv --password='p4ssw0rd' -f csv  protectedFile.xls

and then parse the csv file. Or export to another xls if you need the formatting or want to torture yourself

N.B. Edited after accepted. (--password is the correct switch, not -p, as noted by @enharmonic)

I've recently had an easier time using xlsxunpass

java -jar ./xlsxunpass.jar protected.xlsx unprotected.xlsx 'p4ssw0rd'
Will
  • 1,206
  • 9
  • 22
  • What if my .xlsx have multiple sheets – Máxima Alekz Jan 08 '18 at 00:38
  • try writing to another format supporting sheets. e.g. `unoconv -p p4ssw0rd -f xlsx protectedFile.xls` – Will Jan 09 '18 at 17:28
  • doesn't run (with error)? or is still password protected? if you already have xlsx, try going to xls `unoconv -p p4ssw0rd -f xls protectedFile.xlsx` or ods `unoconv -p p4ssw0rd -f ods protectedFile.xlsx` – Will Jan 09 '18 at 19:33
  • I'm getting this error 'Error: Unable to connect or start own listener. Aborting.' . Any idea why? – Raj Jan 15 '19 at 01:02
  • @Raj I don't think the `-p` option is for passwords; docs say it is for specifying a port. "-p, --port Port to listen on (as listener) or to connect to (as client). Default port is '2002'." https://linux.die.net/man/1/unoconv – enharmonic Feb 26 '20 at 23:14
  • @enharmonic i updated the answer. I wonder if this was a change in the command switches or if the answer never actually worked – Will Feb 27 '20 at 14:54
  • @Will I can unlock the protected file using xlsxunpass package but not unoconv. I'm still getting the same error using unoconv. – Raj Feb 27 '20 at 23:11
  • xlsxunpass is the only thing that has worked for me so far; thanks for your work on it, @Will! – enharmonic Jan 13 '21 at 22:09