How do you launch a python script with LEP?

I asked about splitting multiple page PDF in another thread and I am trying one option using Python that I found futzing around in google.I am trying to run a python script with LEP and am having problems on a Mac (this will eventually be running on windows). The following works in terminal

/pathToThePythonScript/splitPDF.py /PathToADoument/BestInCategory_JR\ 2.pdf 1 2 3 4 5 6 7 8 9

The script splits the pdf and the numbers tell the script how to split the pdf.

So in 4D I first tried to run it like I do in terminal

$execute:= $pathToScript+" "+$pathToDocument+" 1 2 3 4 5 6 7 8 9"
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
Launch External Process($execute)

That did not work. I copied the $execute to the pasteboard, pasted it into Terminal, and it worked fine

So then I tried it like this…

C_BLOB($inputStream;$output;$error)
TEXT TO BLOB($pathToDocument +" 1 2 3 4 5 6 7 8 9";$inputStream)
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
LAUNCH EXTERNAL PROCESS($pathToScript;$inputStream;$output;$error)

I am not sure if the script is running or not. I must be missing something

The script doies have an output, but that blob is empty as is the error blob. The fact that _4D_OPTION_HIDE_CONSOLE is windows only, does that mean that their is no way to show terminal and thus no way to see what is happening?

Any help is greatly appreciated.

Have you tried :

$vt_cmd:="/usr/bin/python"
$vt_inputStream:="some python commands"
LAUNCH EXTERNAL PROCESS($vt_cmd;$vt_inputStream;$vt_output;$vt_error)

or

$vt_cmd:="/usr/bin/python script.py"
$vt_inputStream:=""
LAUNCH EXTERNAL PROCESS($vt_cmd;$vt_inputStream;$vt_output;$vt_error)

?

I tried both, but it is getting late here so I need to try again in the morning.

I am working on a mac. Is /usr/bin/python script.py the correct address. Where is usr?

Hi,

/usr/bin contains some unix binaries pre-installed in your Mac OS X system.

Python should be in there.

I tested this with a python script found here
https://raw.githubusercontent.com/benwiggy/PDFsuite/master/Automator_Scripts/splitPDF.py


C_TEXT($vt_cmd;$vt_in;$vt_out;$vt_err)

$vt_cmd:="/usr/bin/python /Users/ble/Desktop/test/splitPDF.py /Applications/dev/4D/Plug-ins/MyConnect/MyConnect\ Plugin\ v302/MyConnect\ Plugin.pdf"
LAUNCH EXTERNAL PROCESS($vt_cmd;$vt_in;$vt_out;$vt_err)

It works

  1. Open terminal
  2. Type which python
  3. Take the output as the path to your python-executable.

As Bruno stated it should be /usr/bin/python on a Mac.

1 Like

yes, if you are not sure, run

which python

in terminal.

Bruno that script worked perfectly and is much easier to use than the one I had. Thank you! I am good to go. Thanks again.

John

I got Bruno’s script working, but I have a question wrt “which python”. When I first started to work on this when I ran idle.app I got an error regarding ActiveTCL which fixed with instructions I found with google. The fix involved reinstalling Python. In that process I decided to upgrade Python from 2.7.9 to 3.8 which I think I did.

When I execute “which python” I get “/Library/Frameworks/Python.framework/Versions/2.7/bin/python”
and If I execute “python” I get “Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)…”

This is no big deal as everything is working, but I am wondering why it it not using 3.8.

You can try “which -a python”. It will give you all the installed binaries for python.

As I said, by default on OS X you should get a Python interpreter installed in “/usr/bin”. This is done by Apple and you won’t be able to change it (it is a security feature called SIP since 10.11 El capitan).

What you can do is to install another Python (a more recent one for instance) in a different directory (/usr/local/bin for instance) and have your shell to add that directory to the PATH variable. This is done on a file called “.profile” for instance. This is why which will not always give you “/usr/bin”

But if you have installed a different version of python on your machine, it is not necessarily the case on the machines you will deploy. So best approach is to use the pre-installed binaries if possible.

In doubt (if you not sure what python is installed on your machines) you can use

/usr/bin/python -V

HTH

Ok, thanks to Bruno I got my whole process working on a mac. Now I need to get it working on Windows. I tried to figure out the LEP on my own but am not succeeding. A couple of questions…

1. On the Mac for testing purposes I can run the script manually in Terminal by dragging and dropping the script and the target document to the terminal…

MacBook-Pro-3:~ johnbdh2$ /Users/johnbdh2/…path…/splitPDF.py /Users/johnbdh2/…path…/docToBeSplit.pdf
MacBook-Pro-3:~ johnbdh2$

This works perfectly on the Mac but in windows instead of the target document being split, the target pdf opens up in Adobe reader.

Googling I found that to run a script from the command line I should call the script like this…

C:\python27\ python.exe pathToTheScript

So I tried…

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe C:\QDGDocs …Path…/splitPDF.py C:\QDGDocs …Path…\ docToBeSplit.pdf

I got the same result.

So how do I run the script from the command line?

2. On the mac I preface the command with /usr/bin/python, the path to the python install. On the windows machine would it be the same…

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe …

I am going to try that in the morning.

Thanks,

Non

Hi,

Ah, maybe I missed it but you did not mention you wanted cross plateform solution :wink:

Unfortunately the splitPDF python script ( splitPDF.py ) is relying on OS X technologies. It will not work on Windows.

You can always try to find a similar tool/script for Windows but then you are always at risk of both tools not giving consistent results depending on the platform.

So, my recommendation is to take a step back, and find a cross platform tool…

HTH

Bruno, thanks for pointing that out. The script I was trying to use before the one you suggested was Mac only as well.

I found a small script that works with Windows and works well…

from PyPDF2 import PdfFileWriter, PdfFileReader
inputpdf = PdfFileReader(open("<pathToPDF>”, "rb"))

for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open("document-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)


from PyPDF2 import PdfFileWriter, PdfFileReader

inputpdf = PdfFileReader(open(C:\\MySplitFolder\\BestInCategory_JR 2.pdf”, "rb"))

for i in range(inputpdf.numPages):
	output = PdfFileWriter()
	output.addPage(inputpdf.getPage(i))
	with open("document-page%s.pdf" % i, "wb") as outputStream:
	  output.write(outputStream)

You have to install PyPDF2… from the command line… pip install pypdf2.

For my purposes I put “pathToPDF” where the PDF to be split would go, to be replaced on the fly with the pdf a user picks to be split, creating the py script on the fly…

$fileName:=Select document("";"pdf";"Select the PDF document to split...";0;$aSelectedPaths)
$pythonScriptFileName:="splitPDFwindows.py"
$splitFolderPath:=Substring($aSelectedPaths{1};1;Position($fileName;$aSelectedPaths{1})-1)

//put the script in a text variable
$script:="from PyPDF2 import PdfFileWriter, PdfFileReader\r\rinputpdf = PdfFileReader(open(\"<pathToPDF>\", \"rb\"))\r\rfor i in range(inputpdf.numPages):\r    output = PdfFileWriter()\r    output.addPage(inputpdf.getPage(i))\r    with open(\"document-page%s.pdf\" % i, \"wb\""+") as outputStream:\r        output.write(outputStream)"

$script:=Replace string($script;"<pathToPDF>";$aSelectedPaths{1})

//create the script file...
TEXT TO DOCUMENT($splitFolderPath+$pythonScriptFileName;$script)

SET ENVIRONMENT VARIABLE("_4D_OPTION_CURRENT_DIRECTORY";$splitFolderPath)
SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"true")
LAUNCH EXTERNAL PROCESS("cmd.exe /C start \"\" \""+$pythonScriptFileName +"\"")

Thanks to all who helped!

John

Probably not the right forum, but I have alreaddy shared the script I am using in this one, maybe someone can tell me what might be wrong. The script works with one document but with another I am gettin the following error…

Traceback (most recent call last):
File “C:\MySplitFolder\splitPDFwindows.py”, line 3, in
inputpdf = PdfFileReader(open(“C:\MySplitFolder\1973PB1PhyOfficeParkingBldg_CD.pdf”, “rb”))
OSError: [Errno 22] Invalid argument: ‘C:\MySplitFolder\x01973PB1PhyOfficeParkingBldg_CD.pdf’


This is line 3 of for the working document (91kb with 9 pages)…

inputpdf = PdfFileReader(open("C:\MySplitFolder\BestInCategory_JR 2.pdf", "rb"))


Here it is for the document throwing the error (1.62gb with 232 pages)…

inputpdf = PdfFileReader(open("C:\MySplitFolder\1973PB1PhyOfficeParkingBldg_CD.pdf", "rb"))

As you can see the documents are very different in size, but the error does not say anything about size.

I’m going to drop in on StackkOverflow with this question.

Thanks,

John

Ooops. I see that I pasted in the script code twice earlier and I can no longer edit it. Pleae disregard the second half of the posted script. It should have looked like this…

from PyPDF2 import PdfFileWriter, PdfFileReader
inputpdf = PdfFileReader(open("<pathToPDF>”, "rb"))

for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open("document-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)

OK, I think I figured it out. I did not notice the \x just befoe the file name. Python is interpreting that as an escape character. So it sees \x197 which I think is Å, so the invalid argument. The easiest solution was to put an r just before the path which tells python that what follows is raw text…

inputpdf = PdfFileReader(open(r"<pathToPDF>”, "rb"))

The large file works now.
John