Log in

View Full Version : Pyhton Script Called by xlwings Addin



branches
10-29-2017, 09:22 AM
I am attempting to use a basic example of xlwings. I have the following VBA script:



Sub Test()
RunPython ("import VBAtester; VBAtester.test()")
MsgBox ("Script has run.")
End Sub


Which is in the same directory as the python script VBAtester.py:

import xlwings as xw
def test():
wb = xw.Book.caller()
wb.sheets[0].range('A1').value = 'TEST VALUE'


The VBA script runs and outputs the message box; but *'TEST VALUE'* does not show in cell A1, or elsewhere. I know the python script is being called as if I purposefully put errors in my python script VBA will callback these errors when I run the VBA script that calls the python script.

What is the error in my understanding or my approach?

Moderator Edit: https://www.xlwings.org/

YasserKhalil
10-29-2017, 09:26 AM
Cross-Post at this link
https://www.excelforum.com/excel-programming-vba-macros/1206579-no-output-from-xlwings-runpython-macro.html

SamT
10-29-2017, 05:26 PM
VBA doesn't do scripts. VBA does source code. It compiles.
vbs does scripts

As far as your python script, I don't know if anybody here uses Python. Wait and see

However
wb.sheets[0].range('A1').value

Might need to read
wb.sheets[1].range('A1').value

Even better would be to pass the Name of the Worksheet, the numerical index depends on it's tab position.

Also, I am clueless if 'A1' will reach the Excel Workbook as "A1". The lack of capitalization of the Collections will probable by OK.




What is the error in my understanding or my approach?Hard to say... You haven't given us your entire "approach."

leciafosl
06-29-2021, 06:57 AM
I am new to python, could someone help with the same example of xlwings?

SamT
06-29-2021, 09:32 AM
xlwings is a third party App for Python. See: https://www.xlwings.org (https://www.xlwings.org/)