1

I am trying to password-protect an MS Excel file via Python. I found so far that this is not possible via common Excel libraries such as openPyXL and XLSX Writer.

I assume this means that I first need to encrypt it. Please correct me if I am wrong or if there is a better approach.

The password-protection bit is the most important, which is not addressed below, unless it's easy to add code to this to that effect.

So far, here's what I have for encryption:

from ezPyCrypto import key
file_in = r'P:\Data\sample_file.xlsx'
file_out = file_in[:-5]+'_enc.xlsx'

f_in = open(file_in, 'rb')
f_out = open(file_out,'wb')

in_str = f_in.read()
out_str = key(in_str)
f_out.write(out_str)

f_in.close()
f_out.close()

However, I keep getting this error:

ImportError                               Traceback (most recent call last)
<ipython-input-36-ed5218b0d47c> in <module>()
      2 #https://github.com/sfbahr/PyCrypto-Wheels
----> 3 from ezPyCrypto import key
      4 file_in = r'P:\Data\sample_file.xlsx'
      5 file_out = file_in[:-5]+'_enc.xlsx'

C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\site-packages\ezPyCrypto.py in <module>()
     70 from Crypto.Util.randpool import RandomPool
     71 from Crypto.Util.number import getPrime
---> 72 from Crypto.Cipher import ARC2, Blowfish, CAST, DES3, IDEA, RC5
     73 from Crypto.Hash import MD5
     74 

ImportError: cannot import name 'IDEA'

I tried downgrading to earlier versions of Crypto (back to 1.0.0) but keep getting the same error, which looks for 'IDEA' within Crypto.Cipher.

Thanks in advance!

Dance Party2
  • 7,214
  • 17
  • 59
  • 106
  • Instead of entrypting, you could password protect sheets or the entire workbook. Of course it depends on the "level" of encryption you want, but if you are not aware that you can protect a workbook / sheet with a password it might be helpful in some situations – dgorti Jan 24 '17 at 00:29
  • @dgorti I think as long as it would prevent anyone from being able to see the data itself, that would suffice. Is there a way to do so via Python? – Dance Party2 Jan 24 '17 at 14:32
  • @DanceParty2 How did you solve this ? Can you please put an answer – skybunk Jun 22 '18 at 16:41
  • Unfortunately no. :( – Dance Party2 Jun 22 '18 at 18:21

1 Answers1

1

I did not find the package "ezPyCrypto", But I tried this answer: answer 3 in this page and it works great

davidbrown
  • 434
  • 4
  • 5