I am trying to run python script from VBA using shell. below is part of the sub and also the python codes. Program goes to the VBA function - ShellRunPython but it does not run python script. It does not execute the print in the python last statement.
My second issue is that assuming the python script is run successfully later, I need to get back value of PDFtext to variable str1 in sub. Thank you for suggestions
Sub RunPythonFromVBA()
'............
'............
'Specify File Path
sfilepath = Application.ThisWorkbook.Path & "\"
pythonfilename = "CDAvsFAscript.py"
xFilepath = Application.ThisWorkbook.Path & "\"
xFilepath = Replace(xFilepath, "\", "\\")
'Check for back slash
If Right(sfilepath, 1) <> "\" Then
sfilepath = sfilepath & "\"
End If
sFilename = Dir(sfilepath & "*.pdf")
Do While Len(sFilename) > 0
If Right(sFilename, 3) = "pdf" Then
'Display file name in immediate window
If sfilepath & "\" & sFilename Like "*052835022*" Then
xFilepath = xFilepath & sFilename
ShellArg = "python " & sfilepath & pythonfilename & "" & " " & xFilepath
Debug.Print ShellArg
str1 = ShellRunPython(ShellArg)
'................................
'................................
'................................
End Sub
Public Function ShellRunPython(sCmd As String) As String
'Run a shell command, returning the output as a string
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
'run command
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec(sCmd)
Set oOutput = oExec.StdOut
'handle the results as they are written to and read from the StdOut object
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
sLine = oOutput.ReadLine
If sLine <> "" Then s = s & sLine & vbCrLf
Wend
ShellRunPython = s
End Function
..................... ..................... Python script
import io
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
codec = 'utf-8'
laparams = LAParams()
with io.StringIO() as retstr:
with TextConverter(rsrcmgr, retstr, codec=codec,
laparams=laparams) as device:
with open(path, 'rb') as fp:
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos = set()
for page in PDFPage.get_pages(fp,
pagenos,
maxpages=maxpages,
password=password,
caching=caching,
check_extractable=True):
interpreter.process_page(page)
return retstr.getvalue()
if __name__ == "__CDAvsFA__":
#Filepath = 'C:\\passport all paged scan\\testPDF.pdf'
PDFtext = convert_pdf_to_txt(xFilepath)
print(PDFtext)
xfilepathandsfilepath?xFilepathdoesn't seem to be a variable that exists in your python script? How are you getting the value for that?