0

I have a python script for converting PDFs to images. I have tested it and found that it works, but I would like to call it within a poweshell script.

The powershell script I have seems to actually run, but doesn't actually do anything. It is:

$env:Path += ";C:\Program Files (x86)\Python37-32"; #path to where I have python installed
$env:PATHEXT += ";.py"

$arg1 = 'C:\Localdata\TestPath' #path to target PDF
$arg2 = 'sample.pdf' #tried both double and single quotes here, doesn't seem to help 
$arg3 = 1 #starting page to convert
$arg4 = 2 #ending page to convert
$arg5 = 'C:\Localdata\testpath' #path to save jpg
$arg6 = "temp" #used in jpeg name along with page number
$arg7 = 'C:\Localdata\AutoPQR\PDF2Image.py' # path to python file


python $arg7 $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 

The python script is:

def PDFtoImage(targetpath, PDF, startpage, endpage, savepath, savename):
    os.chdir(targetpath)
    startindex = startpage-1
    endindex = endpage-1

    i = startindex

    pages = convert_from_path(PDF, 0)
    while i<endpage:
        pages[i].save(savename + str(i+1) +".jpg" , 'JPEG')
        i = i+1

Am I being daft or missing something? Please provide me suggestions, thanks!!

*Update, upon the advice of those in the comments I have tried the following, unsuccessfully

Start-Process -FilePath 'C:\Program Files (x86)\Python37-32\python.exe' -ArgumentList 'C:\Localdata\Testpath\PDF2Image.py' 'C:\Localdata\Testpath' "sample.pdf" 1 2 'C:\Localdata\Testpath' "temp"

and

Start-Process -FilePath 'C:\Localdata\Testpath\PDF2Image.py' -ArgumentList 'C:\Localdata\Testpath' "sample.pdf" 1 2 'C:\Localdata\Testpath' "temp"
Andy
  • 207
  • 4
  • 18
  • Try Start-Process, it accepts -ArgumentList as a string array and you can specify full path to executable in -FilePath. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6, – Vladimir Dronov Nov 25 '19 at 01:04
  • 1
    Your Python code shows no attempt to access command-line arguments and pass them to your `PDFtoImage` function - is that the part you're missing? – mklement0 Nov 25 '19 at 02:33
  • 1
    @VladimirDronov: There's nothing wrong per se with how `python` is being invoked here - _synchronously_, by _direct invocation_. Unless you want to run a program _asynchronously_, possibly in a separate window, do _not_ use `Start-Process` - see [this answer](https://stackoverflow.com/a/51334633/45375). – mklement0 Nov 25 '19 at 02:36
  • @mklement0 Oh my, I didn't even realize I had to do that. Thanks to your answer I got it working. THANK YOU!!! – Andy Nov 25 '19 at 02:51
  • Glad to hear it, Andy, but it was the post that someone else had pointed out (unfortunately, I don't see their username any longer); anyway, I've marked your answer as a duplicate of said post. – mklement0 Nov 25 '19 at 02:56

0 Answers0