I am using mac OS X Sierra with Mac for Ecel 2016 and python 3.6. I am trying to launch a python script from excel because:
I already have an excel with a lot of functionalities and I would like to add some features to this product using Python.
The users of my product won't be comfortable launching python scripts from a python console, so I'd like to have some kind of excel user interface.
I am providing this background because I am open to suggestions on how to do differently. From my researches so far, it seems that this can be done using the xlwings library. I have tried using it following the instructions on:
http://docs.xlwings.org/en/stable/vba.html#vba
I am somehow stuck at this stage:
Open the VBA editor with Alt-F11
Then go to File > Import File... and import the xlwings.bas file. It can be found in the directory of your xlwings installation.
As alt + F11 doesn't open the macro editor, I have understood the instructions as follows:
click on the 'macros' button in the 'developper' tab of excel
create a new macro / edit an existing one
go to 'add references' on the bottom left corner
open the file mentioned
The issue is that I can't open the xlwings.bas file ; I can only open some of the files. I managed to open a file called xlwings.xlsm ; that creates a new tab called 'xlwings' on Excel, in which there is one button: 'import Python UDFs'. When I press this button it throws the error :
Run-time error '53':
File not found: xlwings64.dll
In one of my previous attempts using xlwings, I had a different error at this stage. I cant remember it, but from my researches the solution implied doing something on the trust center of excel 2016 which I couldn't do with Excel for Mac 2016.
Alternatively, when I try to run a macro using RunPython, it throws the following error :
Compile error:
Sub or Function not defined
Pointing at RunPython...
If someone knows what I did wrong and/or how I can manage to use xlwings, that would be great.
Thank you for the help,
For your information, the best solution that I found (thanks to Xukrao) was to download an example from xlwingsexamples, go to the macro editor, copy the "xlwings" module that is in the example worksheet and paste it in the macros of my worksheet.
The fastest way to create a new project according to https://docs.xlwings.org/en/stable/command_line.html is to use the CLI
xlwings quickstart myproject
This will create an .xslm and a .py file.
Related
this is my first time asking a question on this forum, so, any tip or suggestion is highly appreciated!
As for the question itself, I have already seen many discussions on how to export a Colab notebook as a pdf, however I would like to ask more specifically if there is any way of doing it that can preserve the output of executed code (e.g.: I would like tables made from dataframe in pandas to be exported as they were printed on the notebook and not like a bunch of strings).
I think the easier method is you can use browser print functionality.
for most browser it's shortcut should be ctrl + p
and the the harder method is that you can download ipynb file to your machine and then use jupyter notebook to do this
for this to work you should install notebook-as-pdf pip package and then you need to use this command in your command-line or terminal
pyppeteer-install
after that you are all set, so now you can open your ipynb with jupyter notebook and you should find "PDF via HTML(pdf)" option in "download as" section of file menu
in other word it should be here:
file > download as > PDF via HTML(pdf)
if you want more details on this use this and this.
It must be very simple, but somehow I am missing something... Apologies for the long message but it's driving me crazy.
I'm reading thinkstat2, a free pdf about statistics for data science (beginner level).
It comes with exercises so I'm trying to follow the instructions and import the data. I'll describe the steps I'm following and give the pages, it's like a 2 minutes read.
Step 1a: Page 8 - "0.2 Using the code" - Get the code from author's github. I don't have a github account, so I dl the code as a zip on my hard drive. Then I extracted the content in a new folder.
Step 1b: Page 9 - Author suggests to get anaconda. From my understanding, anaconda contains many different app you can use through the "anaconda navigator". I have anaconda and will use Jupyter notebook for this.
Step 2: Page 9 - Author asks to run "nsfg.py" from his zip to make sure the reader has all the packages installed. Author says I should have a confirmation message. When I run it, there's a flash of command screen without any error or confirmation message. But if it's all about checking that the right package are installed correctly, I can just add "import x" in my code to add the missing package, so not a real issue here (or so I believed). I moved on.
Step 3: Pages 24 to 26 - "1.3 Importing the data" and "1.4 Dataframes" - This is where I'm stuck. After running "nsfg.py" (I mentioned in step 2), the author asks to "import nsfg", which is his own package created from "nsfg.py"? Maybe? So I try to import it and it's not recognized. Which probably comes from that "not a real issue" in step 2... I checked inside "nsfg.py" and it seems to be code to clean and read a .dct and .gz file.
Questions:
I don't understand what I'm supposed to do to make "nsfg.py" run properly.
I don't understand how my jupyter notebook would understand the "import nsfg" since those lies on my hard drive. The author does not add any line regarding importing anything from a local drive, or from a url to his github (not in the code of nsfg.py as well, except if I missed it).
I thought about just copy pasting the code of "nsfg.py" and running it, but I believe it would not work since the .dct and .gz file mentioned are saved locally as well. So I don't get how the code pasted from nsfg.py I run on jupyter notebook would make a link with those local .dct and .gz files. My python is not advanced enough to tinker that manually...
Help :( (any alternative solution is welcome)
Two ways to solve this, both worked for me:
Firstly, to replicate what the extracts of the book say, all assuming you are using linux or mac:
Hit git clone https://github.com/AllenDowney/ThinkStats2.git in your terminal in whatever folder you want to wok in
Then cd ThinkStats2/code.
Then python nsfg.py
Then hit python again to give you an interactive shell which is what the author of the book appears to be using - note the >>> before each command which indicates that it isn't in a Jupyter notebook.
To get this to work in a Jupyter notebook:
Hit git clone https://github.com/AllenDowney/ThinkStats2.git in your terminal in whatever folder you want to wok in
In your jupyter notebook interface, create a new notebook in the same directory as the ThinkStats2/code folder.
In the notebook hit import nsfg.
I think you are probably getting errors because you are not running your notebook from the same directory as the nsfg.py file which means that python can't find the script and so can't import it.
Python3: How do I import an excel spreadsheet into python project? (I'm using repl.it website for learning python3). I want to automate the entering of data into several connected spreadsheets. I'm trying to automate my work so that I don't have to do it manually anymore.
You can process Excel files directly if you use a local install of Python and a library like OpenPyXL or others.
However, as a workaround, in Excel save the file as a .csv (comma separated values). Open that .csv in a text editor. Copy the contents. In repl.it, click the new file button and create a new file called something like input.csv and paste in the contents.
Repl.it does have the csv library since that's a native library. Details are at https://docs.python.org/3.6/library/csv.html. That should let you read in the data fairly easily.
Since you are dealing with several connected spreadsheets, you may have to do this step with each one and create a new .csv file as your result which you can then open in Excel and save as an Excel file. However, you really are pushing the bounds of what can be done in an online repl.
If you are dealing with large files or want to skip the "save as csv" step, you'll need a local installation of Python.
I'm trying to install IDLE2HTML from the following link in order to print from IDLE in color. I use Python 2.7 on a Mac.
I follow the instructions from the readme.txt, but when I reload IDLE nothing has changed. I don't have the Save As HTML option.
In the past I've installed and reinstalled Python a few times.
When trying to find my idlelib folder (as written in the readme instructions), I eventually found it under the path /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
Is my problem that my python is in /System/Library/?
Any ideas please?
I have not tried this extension, but the following may help. When you start Idle, select File / Path browser on the menu. You should see
_ sys.path
|
+ some_path/pythonxy/Lib/idlelib
|
+ some_path/python27.zip # or MAC equivalent
The first path tells you where the new files should be. If you click the +, you will see what file are there. What about IDLE2HTML.py? The next issue is whether you properly edited config-extensions.def. If you wait a few days for 2.7.9 and install it, you can try the new Options / Config-extensions dialog on the menu. That will show whether Idle can read the new information in the .def file. Or you can edit your question, paste in the new material you added, and comment on this answer, and I will take a look. However, given that the extension has been around for years, I presume that the addition in config-extensions.txt is correct.
I suspect that whenever you upgrade Python, config-extension.def will be overwritten and you will have to re-edit it.
If the problem is not a configuration problem with the IDLE2HTML Python IDLE extension like this answer (for the same question) explains about, it may be a problem with the code itself (this has happened with Python 3 IDLE). This can be checked by running IDLE using the Python file that starts it (<PYTHON LIBRARY DIR>\idlelib\idle.py where <PYTHON LIBRARY DIR> is the location of the standard library for the Python installation), in a command line interpreter (use <PYTHON EXECUTABLE DIR>\python.exe where <PYTHON EXECUTABLE DIR is the location of the executable files that start Python). It will launch IDLE and show any errors in the C.L.I..
Quote from Python idlelib module docstring:
Idle includes an interactive shell and editor. Use the files named idle.* to start Idle.
IDLE2HTML real error example:
A common problem is that IDLE2HTML version 2.0 (latest at time of writing) needs a simple code tweak to work with Python 3.x (a fixed version that works as part of the IDLEX Python module had this tweak). File comparison image (left is original file, right is IDLEX version in image); if the Python version is Python 3.x, import Tkinter needs to be changed to import tkinter as Tkinter and import tkFileDialog needs to be changed to import tkinter.filedialog as tkFileDialog:
Edwards suggestion of using the CLI for diagnosis was very useful. It exposed that the cgi.escape attribute does not exist for Python 3.
I changed the IDLE2HTML.py line 74 from "out.append(cgi.escape(content))" to
"out.append(content)" and after a re-boot it all worked!
Later: well almost... better to replace "import cgi" with "import html and then cgi.escape() with html.escape()
If using Win10 you will need super-admin rights to change IDLE2HTML.
I am using Enthought canopy for data analysis. I didn't find any option to create a .py file to write my code and save it for later use. I tried File> New >IPython Notebook, wrote my code and saved it. But the next time I opened it within Canopy editor, it wasn't editable. I need something like a Python shell where you just open a 'New Window', write all your code and press F5 to execute it. It could be saved for later use as well. Although pandas and numpy work in canopy editor, they are not recognized by Python shell (whenever I write import pandas as pd, it says no module named pandas). Please help.
Umair, ctrl + n or File > Python File will do what you want.
Best,
Jonathan
Let me add that if you need to open the file, even if it's a text file but you want to be able to run it as a Python file (or whatever language format) just look at the bottom of the Canopy window and select the language you want to use. In some cases it may default to just text. Click it and select the language you want. Once you've done that, you'll see that the run button will be active and the command appear in their respective color.