2

I am trying to use python to open a pdf file on my computer. The PDF file is locked. I know the password, but I have no idea how to enter the password once the page appears using python.

I currently do the following:

import os
os.startfile('file.pdf')

enter image description here

assuming the password is for example 'abcd', could somebody please tell me how to type into the password prompt box.

Thanks

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Parky118
  • 33
  • 2
  • 1
    Maybe there's an easier way by passing the right parameter through the CLI. What pdf reader are you using? For Adobe Reader check [this answer](https://stackoverflow.com/questions/619158/adobe-reader-command-line-reference). – MGP Aug 22 '18 at 20:15
  • As @ManuelGutierrez indicated, it all depends on what PDF reader you're using to open your document. – esqew Aug 22 '18 at 20:17
  • You may want to have a look at [this question](https://stackoverflow.com/q/45795089) to read the PDF in Python and decrypting it from python. Then it should be possible to write a temporary unencrypted copy of the PDF which you should be able to open without password query. – Adrian W Aug 22 '18 at 20:21
  • I am opening the PDF on Microsoft edge – Parky118 Aug 22 '18 at 20:23
  • @Parky118 Can you afford to use another one? With [Foxit Reader](http://forums.foxitsoftware.com/forum/portable-document-format-pdf-tools/foxit-reader/774-portable-version-command-line-options-help-needed) it's simple enough. – MGP Aug 22 '18 at 20:30

1 Answers1

0

As per the comment above, you will have an easier time passing in the password as a parameter to the CLI. However, I have a hunch that a proper CLI is unavailable for the reader you're using (most of my favorite GUI desktop applications have no proper CLI :( ).

If the CLI is indeed not an option, your next best bet is to use SikuliX. It's a Jython (Python that runs on Java) app that let's you quite easily script GUI interactions.

In this case you would use the SikuliX app to screenshot the password prompt, and then your script would navigate to the prompt, paste the password, and then either hit the return key or press the Enter key with the mouse (you will need to also screenshot the Enter key). I've used SikuliX in the past to convert a bunch of .gpx files to .midi using Guitar Pro, and if you put in some error-handling for edge cases it actually does a really good job, even with very simple scripts.

vasia
  • 1,093
  • 7
  • 18