I am working with xlwings v0.25.0 and have the matching version for the Excel add-in. I am also using Office 365 and Python 3.7.4.
I am trying to run a Python script, testing_xlwings.py from a vba macro in Excel.
import xlwings as xw
def main():
sheet = xw.Book.caller().sheets[0]
sheet.range('B10').value = 'IT WORKED!!!'
I have assigned the macro to the button and when I click to run, I receive the following error Compile error: Sub or Function not defined.
My sub for the macro looks like this:
Sub Run_xlwings()
RunPython "import testing_xlwings.py; testing_xlwings.main()"
End Sub
If I change sheet = xw.Book.caller().sheets[0] to sheet = xw.Book('testing_workbook.xlsm').sheets[0] the script executes and the text is placed in the Excel file.
I have also downloaded the Monte Carlo example from the xlwings examples page. When I open the file and try to run the .py script using the macro, I receive this error: AttributeError: module 'xlwings.utlis' has no attribute 'prepare_sys_path'
What do I need to change to run the script from the macro?
I was able to get the macro to the call the Python script by pressing Alt + F11 to bring up the VBA editor. From the toolbar across the top, I selected Tools -> References and I ticked the checkbox for xlwings. After I clicked OK I was able to run the script.
if you still get error... try to install a previous version of xlwings.
On the console type:
pip install xlwings==0.27.2
xlwings addin install
Related
Thanks for your time. Im an old programmer coming back to Python
I have a new install of Anaconda3, Windows 10 and Microsoft Office 2010
I tried install xlwings via pip straight into Python3 - didn't seem to work
Ive then installed anaconda3, spyder.
Ive installed inside anaconda at the command prompt
xlwings addin install
xlwings addin update
initially I got an error going into excel, saying 2 spreadsheets of the same name can not be open - I guess that was something to do with when I was trying to get the original python3 / pip/ addin manager to work....
Ive spent all day trying to sort that. It no longer appears in the addin menu (developer), just in the ribbon - xlwings
Ive run a program as per the documentation spyder/python and get Hello xlwings inside excel (created the python and excel spreadsheet with the xlwings quickstart myproject)
But when I try and use a fuction that sends data to python I get an error. if I click on run main, import functions or restart UDF server I get a missing DLL error
Run Time error 53 - file not found xlwings32.0.19.2.dll.
ive tried copying it to System32, removing the addin (xlwings addin remove) and reinstalling it
I completely unsinstalled Microsoft office and reinstalled it - the addins was still there anyway !
Sorry a lot of explanation for what is actually a DLL issue, but ive been going around and around for hours.
So in summary, I don't had xlwings under the addins/developer in excel, but it does appear on the ribbon. I can run a python program that puts "hello world" into excel but excel does not recognise the python function and when I try and interact with excel I get a missing .dll error.
What should I do ? rip out python / accoconda etc and start all over again ?
cheers
Older and Greyer !
I don't know if this will fix your problem, I was having the same error with Excel's "sorry you can't have two workbooks of the same name open at the same time" and solved it. The error only happened when the Excel add-in "xlwings" was checked. I fixed the error by moving the location of the xlwings add-in from C:\Users\MYNAME\AppData\Roaming\Microsoft\Excel\XLSTART where it had automatically put itself to C:\Users\MYNAME\AppData\Roaming\Microsoft\AddIns.
Dear Stackoverflow members
I've been trying to install and use the xlwings addin for several days now but I am not able to get it running... I really would like to use xlwings, but it is being painfully difficult.
I am using Anaconda.
xwlings is installed and works perfectly (I can read/write from/to an .xlsx file) but what I want to really do is to call python functions from excel and for that I need the addin.
The docs are not super useful here and I've tried to follow this guide (from https://newtonexcelbach.com/2017/07/30/installing-xlwings-0-11-4/):
" Run xlwings addin install from a command prompt. Reopen Excel and check if the xlwings Ribbon appears. If not, copy xlwings.xlam (from your xlwings installation folder under addin\xlwings.xlam manually into the XLSTART folder. You can find the location of this folder under Options > Trust Center > Trust Center Settingsā¦ > Trusted Locations, under the description Excel default location: User StartUp. Restart Excel and you should see the add-in"
When I opened excel I did not see the addin. I put the xlwings .xlam file into the XLSTAR folder... reopened excel and the addin was still not there. Then I put the .xlam file also in this folder : "C:\Users\UserName\AppData\Roaming\Microsoft\AddIns".
The .xlam file is now in both folders, and now I can see the xlwings addin.
then I create a folder with myproject.xlsm and myproject.py files by running "xlwings quickstart myproject" from the anaconda promt. The files are there but then when I try to call any python function/Script from the vba modules I always get this error:
"Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\users\joanc\myproject\myproject.py", line 1, in <module>
import xlwings as xw
ModuleNotFoundError: No module named 'xlwings'"
for example, I get this error when I try to run the code that is already available when myproject.xlsm is created. The code looks like this:
sub SampleCall()
mymodule=Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".",-1, vbTextCompare)
RunPython("import " & mymodule & ";" & mymodule ".hello_xlwings()")
end sub
I tried to run this code directly from the developer tab and also from a button created in sheet1 and assigning its corresponding macro...
As a side note if I go to the developer tab>tools>references i can see the xlwings checkbox active... so this should not be a problem.
Well, this is all I've done so far... no idea what I am doing wrong and what to do next. I would really like to use this package but if anyone knows a good alternative I am totally open (not only to read/write from/to excel but also to call python scripts/functions directly from excel)
Thank you very much for your help
I want to write a VBA macro in Excel to call a python script. The python script will execute some commands then return a pandas dataframe. I have code that writes to the excel spreadsheet and that is inside the python script.
I have installed xlwings and read through the VBA macro tutorial but am still confused. Should I use a User Defined Function? Do I need to "return anything" in the script?
If your Python script writes to the workbook directly, this is not difficult. First, you don't need to return anything from either the VBA macro or the Python if what you write to the workbook is sufficient for you.
You don't need a UDF. All you need is the following (which I've basically copied from the xlwings official docs here):
1) Make sure your excel book can talk to the file you have your script in (best done using the command xlwings quickstart <project_name> on the command line, then copying your script to the resulting Python file.
2) Make sure you can call everything you need from a single function like __main__() (or be prepared to run execfile or something similar)
3) Go to your excel book and make a command button, then assign it the macro SampleCall which xlwings has kindly provided for you
4) Now open the VBA editor. You should see the SampleCall macro immediately:
Sub SampleCall()
RunPython ("import testproj;testproj.world()")
End Sub
Just edit the names of testproj and world appropriately.
You can now click your CommandButton to execute your script!
I have a Python Script (with .py extension) and it was created by makepy.py.
Is there a way to view and copy the codes inside that script and load them into my Jupyter Notebook?
I have done a Google search but strangely, I can't find this mentioned anywhere.
Do I need a specific software to do this? Or can it be done at the Python command prompt level?
You can just edit the file with a text editor. Right click and select open with. Then you can copy the code.
All that can be done directly from the Jupyter Notebook or any IPython console.
to view the contents of the script
!cat myscript.py
to run the script as a program use this built-in magic command
%run myscript.py
You can load file in Jupyter notebook cell by:
%load your_file.py
You can do changes to it and than save it back by
%%writefile your_file.py
or
%%file your_file.py
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.