Python XLWings Anaconda Newbie - Install / Basic issues - python

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.

Related

Python code invokes different interpreter from shell

During zerphyr rtos installation process i ran into this error https://pastebin.com/8F5A8S8m, which i safely brute-forced by the link given in the next sentence, but now same error but different file. And here is the code from that file https://pastebin.com/EawPKXRG. So, in short, by googling i couldn't find a way to change interpreter that python uses in this particular files.
FYI
which python
gives python 3.11 one
sudo su
nano etc/path
gives also right directory for python 3.11, but somehow these files use python 3.9 which is installed on this mac by default.'
p.s sorry for the grammar and structure, I'm so mentally drained now to write it properly.
Didn't find anything on google
So you need to run those python files in this format
python3.11 your_desired_file_name

Cannot call Python script from macro using xlwings

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

Errors using pyinstall to create exe file, last error: 'ImportError: Unable to load module attribute'

I have a simple .ipynb that reads a value from one excel file, and prints it to a cell in another excel file. I use 'jupyter nbconvert --to script MyFile.ipynb to generate .py file. This works, no error. .py file is created.
In terminal:
C:\Users\me\Anaconda3\Scripts>jupyter nbconvert --to script MyFile.ipynb
Output:
[NbConvertApp] Converting notebook MyFile.ipynb to script
[NbConvertApp] Writing 1034 bytes to MyFile.py
I then go:
C:\Users\me\Anaconda3\Scripts>pyinstaller --onefile MyFile.py
I then get:
raise ImportError('Unable to load module attribute') from e
ImportError: Unable to load module attribute
This is my stop point now, and I can figure it out. I find information about adding paths etc., but I don't really understand what to do?
The history is like this, I first got 'maximum recursion depth exceeded error'. I tried to solve this by altering spec file with:
import sys
sys.setrecursionlimit(5000)
I then ran -C:\Users\me\Anaconda3\Scripts>pyinstaller --onefile MyFile.py' again, with no luck. Spec file was over-written.
I then ran 'pyinstaller MyFile.spec' this did not work.
I then found somewhere that the version of openpyxl could be the issue, and I uninstalled current version, and installed openpyxl 2.3.5. I then deleted my .py file and started over to be on the safe side. After doing this I got the above 'ImportError: Unable to load module attribute' error.
It seems like I keep moving towards getting an exe file in the end here, but it sure isn't as easy as some forums would like it to be.
What am I doing wrong? How can I improve my overall code/ setup to reach my goal of a functional exe file? I am using windows, and the users of the exe file will be windows based.
Thanks!
I was having a similar issue and for me the cause of my version of matplotlib. There appears to be a current known bug with matplotlib version 3.3.x. Downgrading to 3.1.2 fixed my issue. Also if you are using Anaconda Navigator I would recommend creating a dedicated Python environment solely for packaging your script.

unable to succesfully run xlwings addin

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

Xlwings fails to import UDFs

So, I have an Excel workbook that I originally created on Windows 7 running Anaconda 4.1.1 (Python 3.5) and Excel 2013. Everything was working great in that environment. I am now trying to work on it remotely using a computer running Windows 10, Anaconda 4.1.1, and Excel 2016. I downloaded the workbook and its corresponding .py file, but when trying to import the UDFs in the .py file I get the following error:
(link to image)
If for some reason the link is broken, the error text is basically
ImportError: No module named 'C:\\Anaconda3\\custom_scripts\\loop_parameters'
The file's name is "loop_parameters.py" and it is found in the path "C:\Anaconda3\custom_scripts". It seems to me that somehow the name of the module (loop_parameters) is getting conflated with the path associated with it. But I have no idea how to fix that. I tried changing the UDF_PATH variable with no change in the error message at all. Thoughts?
So I ended up just starting a new project in xlwings on the new machine, copying over the python code into the new .py file, importing the UDFs, then copying the entire workbook from the old (non-functioning) book to the new one, where the UDFs worked fine. I'm still not sure what the error was or why it was happening. The workaround does seem clucky, but it only took a few minutes.

Categories