#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Execute a Python Script from Excel


    Hi,

    I was not sure if this belonged in this forum - so apologies if I have posted in the wrong area.

    I have a Python script which works perfectly and execeutes when I double click in Windows Explorer. I would like to execute the script when I click a button in my Excel spreadsheet.

    In my Excel VBA I have used the following command:-

    Call Shell(pyPath & pyFile)

    pypath ="C:\Python26\python.exe "
    pyFile ="script.py"

    However, when the code runs I can see the Python icon flash up in my task bar but the script does not run/execute, if I exclude the 'pyFile', the Python interface opens. Can you please tell me where I am going wrong?

    Thanks.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    You might not be doing anything wrong. Have your python program exhibit a lasting external symptom.
    Code:
    import time
    time.sleep(8) # hold the window open for a few seconds
    
    # or
    
    with open('a_sign.txt','w') as o:
        o.write('Hello!\n')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You might not be doing anything wrong. Have your python program exhibit a lasting external symptom.
    Code:
    import time
    time.sleep(8) # hold the window open for a few seconds
    
    # or
    
    with open('a_sign.txt','w') as o:
        o.write('Hello!\n')
    Thank you for the reply - I used the code and when I execute my VB script all I see is a 'black' python screen and the script does not execute. The strange thing is that it appears to be an intermittent issue - sometimes the script works and other times it doesn't, but I cannot find where the issue lies as I am not making any changes to the VB or Python scripts.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    66
    Rep Power
    2
    Apply this VBA code to your folders and files. It worked for
    me using Excel 2003.
    Code:
    Private Sub CommandButton1_Click()
    Dim pyPrgm As String, pyScript As String
    pyPrgm = "C:\Python33\python.exe "
    pyScript = "C:\Python33\MyScripts\test1.py"
    Call Shell(pyPrgm & pyScript, vbMaximizedFocus)
    End Sub

IMN logo majestic logo threadwatch logo seochat tools logo