1

I am writing a short script that will loop through the pdf files in a directory, open them iteratively and let me classify them. The code I've written almost does everything I want. It loops through the files and opens and closes them. Note I'm working on Linux.

However, the cursor is not always staying on the shell, and I don't want to have to keep pressing Alt-tab. Does anyone know a way to use subprocess to run system commands, but to never move away from the shell when Python is running?

A simplified version of what I've got so far:

import signal
import os
import subprocess
import glob

files = glob.glob("pdfs/*.pdf", recursive=True)

for ff in files:
    # get the evince call
    command = f"evince --fullscreen {ff}"
    # open the pdf
    process_cat = subprocess.Popen(command, stdout = subprocess.PIPE, shell = True)
    next = input("Keep viewing files? Y/N")
    # close the pdf
    os.kill(process_cat.pid, signal.SIGKILL)

    if next != "Y":
        break
2
  • Have a look at the pywin32 module. I have never tried what you ask for myself but it looks like it is possible: blog.pythonlibrary.org/2014/10/20/…. Commented May 4, 2021 at 10:58
  • Thanks. I should have added I'm working with Linux, though at the minute I'm trying to take a similar appproach to that described in the link Commented May 4, 2021 at 11:00

1 Answer 1

1

idea 1: use of browser

No idea if this will help, but are you wedded to a linux shell approach, would viewing in a web browser perhaps work better?

If so, I was wondering if this package might help?

import webbrowser
files = glob.glob("pdfs/*.pdf", recursive=True)

for ff in files:
    webbrowser.open_new(ff)
    # etc

idea 2: direct display of PNG

Would conversion to PNG and then direct display of the image file from python itself also be an alternative? This post may help in that case: View pdf image in an iPython Notebook

Sign up to request clarification or add additional context in comments.

2 Comments

That would work, mostly. Though in this case I needed finer grained control, such as dual viewing of the pdfs, which my webbrowser doesn't offer.
Just thinking out loud, but what about conversion to PNG and then directly viewing the PNG fromn python? Does this post help? stackoverflow.com/questions/19470099/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.