I just want to keep excel as a UI and allow user call python instead of VBA from the spreadsheet. Also the python files need to be packaged into one .exe file.
Without any tutorials found, this is what i tried:
test.py:
from xlwings import Book
import xlwings as xw
def test():
sh = xw.Book.caller().sheets['a']
sh.range('A1').value = 'hello'
if __name__ == '__main__':
test()
Then I use pyinstaller:
pyinstaller test.py -F
I copied the spreadsheet test.xlsm to the same directory as the test.exe
The vba codes from test.xlsm:
Sub callpython()
RunFrozenPython ("test.exe")
End Sub
Eventually i got: ---------------------------
Error
'test.exe' is not recognized as an internal or external command, operable program or batch file.
What's more annoying is that if I import pandas in python, pyinstaller won't even compiler due to "maximum recursion depth exceeded".
Can anyone provide an example of how to make these two things work together? I don't even have to use xlwings or pyinstaller, as long as it can achieve making python codes into one executable file and run it from Excel.
=======================
Update:
I finally fixed them by:
- uninstall pyinstaller and replace it with pyinstaller development version (v3.3)
- manually modify xlwings.bas code in VBA. It seems that PYTHON_FROZEN searches executable with a hard coded path: PYTHON_FROZEN = ThisWorkbook.Path & "\build\exe.win32-2.7
Hope xlwings team can replace the logic of finding the .exe file with a more robust one.
With some delay... the following spec worked out for us.
The ruses are:
onefile
oronedir
console=True
orFalse
Our spec "MagicHelper.spec" looks like
Please run it with
Finally, give your end users the good Excel experience, they deserve