Decrypt and Save a Text or CVS with PGPY and Python

Hopefully this helps someone to Decrypt and Save a Text or CVS file with PGPY and Python. Use Kleopatra and  OpenPGP to generate and save your key. You can obtain it by Exporting “Backup Secret Keys”.

Then run this code to decrypt and save the file in whatever location you need.

import pgpy

emsg = pgpy.PGPMessage.from_file('YOUR FILE.pgp')
key,_  = pgpy.PGPKey.from_file('YOUR PRIVATE KEY.asc')
with key.unlock('YOUR PGP PASSWORD'):
    print (key.decrypt(emsg).message)
    with open("my_file.txt", "wb") as binary_file:
   
        # Write bytes to file
        binary_file.write(key.decrypt(emsg).message)

That’s it! Keeping it short and simple.