Just as the title goes.
I have a python program which processes some data file I downloaded from email.
I am writing a vba script which can download the email attachments and execute the python program to process the email attachments, such that I can automate my daily job.
Any idea?
One way to do that is to turn your Python code into a COM Server and then access it like any other COM object. This chapter from Python Programming on Win32 shows how simple it can be. The rest of the chapter gets into a lot of technical details that are probably not necessary for your application. Just turn your Python code into a class, expose one method for the VBA to call it, and that should do the trick.
Shell "path to my python exe"
I am sorry, I haven't used python enough.
Lets say, you want to run .pyc file, it could be
Shell "path to python.exe argumentOfPYCFilePath"
Related
I need to run a python script when an excel file gets dropped in onedrive for business. The python script needs to read from that file and then do some actions. I have been searching aroud and I see that azure functions app is the way to go. I wanted to know if there are any other options to get this done? I see in powerautomate desktop that it runs python 2 code? I need to use python 3 Anyway around that? Calling Python script with the variable(file in onedrive) using powershell?
I'm trying to email an output of a Python script that I made, however I want to do this periodically without needing to start the script manually.
At the moment I have an Apple script which runs the appropriate Python script and sends this to multiple people. The Python script uses an API to get some information and prints a message to the stdout, which gets sent to the appropriate people with the following simple bash script:
python3 myprogram.py | mail -s "Subject" myself#mycompany.com
This does the trick, but I want it automated even when my computer is not running.
Is there a standard way of making this possible that I don't know of? Is there a better way of periodically sending emails that I could use together with the script?
If there is a better language for this than Python, please do recommend it, I like to learn :)
Yes, if you need it to be running even when your personal computer is not running, you should take a look at deploying the script to a server.
You can have your own server, or pay to use one. Heroku has a really nice free plan to get started.
Check this.
The user opens an Calc .ODS file.
My program needs to read values from it "live", i.e. while it's open.
The reason for this is it's password protected, and there's no python API for password-protected worksheets.
pyexcel-ods reads a saved file, not an open one.
Python-UNO is the built-in LO API. It offers two different ways to do this. The first is easier to set up although slower. To do this, start LibreOffice Calc listening on a socket. Then open a python prompt and enter import uno. The listening LO instance will be slower both for the GUI thread and for running the API commands from python.
The other way is to open LO Calc normally and then run python macros from inside, for example by going to Tools -> Macros -> Run Macro. Macros can also be run from events such as opening a document, or button or key presses. This is faster because python runs inside the same process as the application rather than needing to interact through a socket.
Either way, on Windows, use the python distribution that ships with LO, as that includes glue code to make it possible to import uno. A disadvantage of this is that you may not be able to install other python packages you might want, for example lxml. Linux integration is better in that the system wide python can load the UNO libraries, sometimes requiring a package to be installed first depending on your distro.
A tutorial is at http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html. The APSO add-on helps run and organize the code.
I'm doing a python class this semester, and I'd like to add graphical user interfaces to my programs in the form of web pages. Partly I can't be bothered to learn Tkinter, partly I'm just challenging myself, and partly I just like coding interfaces in HTML/JS.
I know the basics on creating HTTP servers with SimpleHTTPServer, but what I need is a way for my web pages to fire ajax commands to python scripts on the server, which will then be executes server side, and then receive the script output. Kind of like how Sage does things.
How can I create an extension for SimpleHTTPServer that I can then use to serve up the outputs of python scripts? I need a solution that's very general, so that given any python script I write, I can easily wrap it in some sort of interface, put it in the server's folder, and not have to do anything else but call it with AJAX, and my server will handle the rest.
My current idea is to have a ServerSideScript class that my scripts can extend, with a single function called output. Every script I have should contain a subclass of ServerSideScript named Script. When the server is asked to serve, for example, foo.py, it notices the extension and does something like:
if self.path[-3:]: == ".py":
return getScriptOutput(self.path)
...
def getScriptOutput(self, path):
from path import Script # obviously not going to work, since path is a string
return Script().output()
That can't work for the reason pointed out in the comment, I'm aware of things like the import_module function, but importing seems like an ugly way to do this to begin with.
What's recommended?
Python comes with batteries included; CGIHTTPServer will run Python scripts using the CGI standard:
The CGIHTTPServer module defines a request-handler class, interface compatible with BaseHTTPServer.BaseHTTPRequestHandler and inherits behavior from SimpleHTTPServer.SimpleHTTPRequestHandler but can also run CGI scripts.
It'll run Python scripts as long as they have a .py or .pyw extension, and are executable; other paths mapping to files are treated as regular files and have their content served instead.
You may be interested in the cgi and cgitb modules as well, to help ease CGI script development.
You might want to take a look at IPython notebooks - it essentially gives you a web-based interactive Python shell, which even integrates with matplotlib etc. You can save Python scripts as notebooks, add text inbetween statements etc.
You can also use the nbviewer if you want to share the output, but not the interactivity.
Can I use os.system() or subprocess.call() to execute a Python program on a webserver?
I mean can I write these functions in a .py script and run it from a web browser and expect the program to be executed?
Thanks a lot.
EDIT:
Sorry for all the confusion, I am giving you more background to my problem.
The reason I am trying to do is this.
I have a Python program that accepts an XML file and returns me TTF file.
I run that program in terminal like this:
ttx somefile.xml
After which it does all the work and generates a ttf file.
Now when I deploy this script as a module on web server. I use a to allow user to browse and select the XML file.
Then I read the file data to temporary file and then pass the file to the module script to be executed like this:
ttx.main([temp_filename])
Is this right way to do it? Because at this point, I don't get any error in the log or in browser. I get blank screen.
When this didn't work, I was going to try os.system or subprocess.call
You do not use os.system or subprocess.call to execute something as a cgi process.
Maybe you should read the Python cgi tutorial here:
http://www.cs.virginia.edu/~lab2q/
If you want your cgi process to communicate with another process on your local machine, you might want to look at "REST frameworks" for Python.
So long as your server is configured to run CGI scripts (Apache's documentation for that is here, for example), yes, you can execute a python script from a webserver. Simply make sure the script is in the appropriate cgi-bin/ directory and that the file has executable permission on the server.
With regards to your comments:
You can, if you really want, explicitly allow other folders on the server to run executable code. I don't know what server you're using, but on Apache this is done by setting Option +ExecCGI for the folder you want. Again, see the docs I linked to.
You need to give an absolute path with respect to the server. As an example, a site I develop has the layout: /public_html/cgi-bin/ When I want to access .cgi or .py files, the url for the site is something like http://chess.narnia.homeunix.com/cgi-bin/index.cgi. You can also set up re-directs to certain files if you want.
One way to pass parameters through your browser is to append them to the URL like an HTTP POST method. Here's a good example of doing that.
Is that what you were looking for with your question, or did you want to actually invoke the python script with os.system()?
Yes, I do it all the time. Import as you would do normally, stick your .py in your cgi-bin folder and make sure the server is capable of handling python.
Another option would be to simply create an application on Google's App Engine. That gives you oodles of resources and APIs for Python execution.
http://code.google.com/appengine
I've done it quite a bit in classic ASP on IIS 5 and above. I would have the ASP engine execute python code (instead of, e.g., vbscript (hearkening back to the old days, here)). Behind those asp pages would be python modules written in straight python that could be imported and could execute pretty much arbitrary code. As others have mentioned, the effective user needs to have execute permission on the thing you're trying to execute.