0

I downloaded a python program (PYPDFOCR) that runs through the command line. PYPDFOCR has several dependencies (ghost script, image magick, etc..)

When I execute the program in the command line, it fails. The code executes GhostScript with all its arguments but I get the error that the command is not recognized.

If I go to the command line, I can execute ghost script "C:\Programs.....\gswin64c.exe" and pass the arguments and get the result. However, when I run PYPDFOCR, it fails everytime.

 def _run_gs(self, options, output_filename, pdf_filename):
    try:
        cmd = '%s -q -dNOPAUSE %s -sOutputFile="%s" "%s" -c quit' % (self.binary, options, output_filename, pdf_filename)
        logging.info(cmd)        
        out = subprocess.check_output(cmd, shell=True)

    except subprocess.CalledProcessError as e:
        print e.output
        if "undefined in .getdeviceparams" in e.output:
            error(self.msgs['GS_OUTDATED'])
        else:
            error (self.msgs['GS_FAILED'])

The error I get in the command is "C\Program" is not recognized as an internal or external command, operable program or batch file.

When I print the contents of the command, it shows:

c:\Program File\gs\gs9.16\bin\gswin64c.exe" -q -dNOPAUSE -sDEVICE=j[ecgray -dJPEG=75 -r300 -sOutputFILE="C:\test\a3_%d.jpg "c:\test\a3.pdf" -c quit

Again, I can run the c:...gswin64.exe command without the program.

Any help will be deeply appreciated.

  • Suggestion to avoid Windows path-formatting errors, put the c:\Program File\gs\gs9.16\bin in the Windows PATH and don't use C:\... anywhere in the command to be executed. – belwood May 03 '15 at 14:06

2 Answers2

1

The problem is probably that the space after Program in 'Program Files' is not correctly escaped. Use some function like

def shellquote(s):
    return "'" + s.replace("'", "'\\''") + "'"

to escape your command before starting the process.

(Example function taken from here)

Community
  • 1
  • 1
chtenb
  • 14,924
  • 14
  • 78
  • 116
0

I had the same problem on Windows with windres. It turned out to be a windres problem (MinGW64 4.8.3.20141208 from chocolatey).

I debugged into it and found that subprocess.Popen(cmd,**kw), cmd being a list with first entry the executable's path, mapped to _winapi.CreateProcess(executable, cmd, ...), with executable=None. So the error originated from winapi.

In the Posix branch in subprocess there is

        if executable is None:
            executable = args[0]

which is missing in the windows branch. As a try I setexecutable in **kw: Still the same problem.

Then I found https://amindlost.wordpress.com/2012/06/09/mingw-windres-exe-cant-popen-error/.

Indeed it worked with msys2's ming64's windres.

Roland Puntaier
  • 3,250
  • 30
  • 35