Search code examples
pythonpdfautomationacrobat

How to pass text to search in AcrobatReader using Python?


I want to open my pdf in AcrobatReader along with the text to be searched inside the pdf. Let us suppose I want the text to be searched in a pdf sample.pdf is JohnDoe. Manually we will open sample.pdf and in the search box write JohnDoe. I am able to open the pdf in Acrobat using python but having issue passing the text JohnDoe in the background. This code is opening the AcrobatReader for me.

import os
os.startfile("sample.pdf")

How can I pass my string to be searched internally to it so that when the pdf is opened, it has automatically searched for that string in the pdf and displayed?


Solution

  • I found a workaround for this question. You can use the AcroRd32.exe file which is available in windows at this path C:\\PROGRA~2\Adobe\ACROBA~1\Reader\AcroRd32.exe.

    This executable file takes arguments with which you can invoke the Acrobat Reader via os or subprocess module from python.

    import os
    query="C:\\PROGRA~2\\Adobe\\ACROBA~1\\Reader\\AcroRd32.exe /A \"zoom={zoom}&navpanes=1=OpenActions&search={text}\" {pdf}".format(text=text, pdf=pdf, zoom=zoom)
    

    You can also configure the zooming strength and pass the text to be searched in the pdf. This will open the pdf in acrobat and itself search for the passed text. os.system(query)