The original script was written by James Villemarette and can be found at their Gist here. I've modified it to be able to use with my current setup.
Tested on Windows 10 (21H2) with Python 3.14, pip, pypdf v.6.14.2, ImageMagick v.7.1.2-27, Ghostscript v.10.07.1, and Tesseract v.5.5.0. Also on another system running the same version of everything, except for Tesseract v.5.5.3 (the latest Windows installer can be found on UB Mannheim's GitHub here).
Please, read the following advice in its entirety, specially if you're a beginner.
For the advanced users out there, excuse my verbosity, but some of the following may be of use, so I ask you to read as well. Everybody's feedback is very much appreciated.
jvillemare wrote this script for systems where Tesseract and ImageMagick are already part of the PATH environment variable. If you're not sure what I'm talking about, it's like this: When a program's installation folder is added to the system's PATH environment variable, it basically means that the program's commands can be called directly from the command line without the need to specify the full paths to its executables. ImageMagick's v.7.1.2-27 installer already offers adding its installation folder to PATH, Ghostscript v.10.07.1 installer also does, but Tesseract doesn't. So, for this script to work, you have to choose whether to add Tesseract's installation folder to the PATH environment variable or edit the script yourself, including the full path to Tesseract's executable. That's usually somewhere like C:\Program Files\Tesseract-OCR\tesseract.exe, but it could be somewhere else in your machine.
Personally, I am not averse to adding Tesseract's folder to my system's PATH environment variable, but I understand that this isn't exactly the safest way to do things. Permission errors are possible as well, though I didn't get any. You can deal with the PATH/edit-script issue however you see fit. Again, changing system settings can be trivial (even routine) to advanced users, but I warn beginners reading this that this kind of procedure can be deleterious if you get something wrong. If you're not at ease with this kind of stuff, I recommend you seek help from someone that is both more knowledgeable of system administration and that you can trust. If, for any reason, you can't count on such a person, you could try making this kind of customization in a safe environment like a virtual machine. In any case, to learn more about environment variables you can refer to current Windows documentation available at the Microsoft Learn platform. It's also possible to find further information in portals such as Superuser.
Tesseract's windows installer already provides automatic selection and download of additional language data. If for any reason you ended up without those, you can find them here.
After setting everything up, you can run the script from the command line, PowerShell, or Windows Terminal. I usually set aside a folder just for this script and the PDF file that will be processed. If you're not fond of navigating through the command line, you can usually open a PowerShell instance already in the folder containing the script and the PDF file by navigating to the desired location in Windows Explorer, right-clicking on the folder's background, and selecting 'Open PowerShell window here' from the context menu. I can confirm this is possible if you have PowerShell 7 installed.
The script should run ok however the PDF file is named, but I always rename it to "book.pdf" just to be on the safe side. Enough files lost in one lifetime because of long file names and folder trees...
So, at the command line and from the folder containing the script and the text, just run the script using the Python interpreter. I usually do it like this:
python convert.pySince I set it to process the images at 300 DPI, it could take a while to process the file, but I usually need that much resolution for the books I'm processing. Turn it down to 150 DPI if you don't need this high of a quality. It will reduce the processing time and the size of the temporary files. I prefer not to go bellow 150 nor above 300, diminishing returns and all.
Beware that the process can look stuck at the first minutes, even if you have plenty of RAM and a good CPU. This is normal, so I'd suggest you wait at least 15 minutes before concluding that it's really stuck. Maybe more. A while after that, you will see terminal entries for each page being processed one by one. The script will automatically create a folder with the current epoch time and the name of the PDF file and dump all the temporary files there. The final PDF file will be named as the original file but with '-ocr-combined' appended to the name. You can safely delete the folder with the timestamp on the name after the script finishes running.
I can't really claim any credit for this. Maybe for changing PyPDF2 to its current iteration, pypdf. The original script would be 100% functional if ImageMagick didn't have a different command for image conversion now (magick) instead of the one that used to work when jvillemare wrote the script back in 2021 (convert). I assume it still works if you happen to have an older version of ImageMagick installed. Then again, I managed to figure it out after reading jzacman's comment in the original script.
I believe that not everyone that might want/need this kind of script is too comfortable with command lines and programming languages. I wasn't that comfortable myself not long ago. That's why I might seem a bit too verbose: I managed to learn a lot in the past few years from documentation and code remarks that could feel a bit too long or dull to those more proficient in coding. I'm sharing this as a GitHub repository, instead of just a Gist, because it may be of use to others. It sure was for me! I hope that the repository structure is more conducive to continued maintenance and development. If the people I mentioned above wish to be added here somehow, or want me to take this down, just let me know.