I'm a Python programmer and I was wondering if there's any module or framework that would allow me to interact with native Windows/Linux applications using python.
Let's say on Windows I have MS Outlook, so would it be possible to let's say automate MS Outlook to compose or check new emails?
I understand that Linux and Windows are different, but what I'm trying to achieve is to let's say open up an application, click on a button programmatically, etc.
I'm not sure if all of these are at all possible using python, so please enlighten me regarding what you think is possible and practical.
For something like Outlook, the best bet is probably using Outlook's COM (Component Object Model) with Python COM libraries.
Most applications have some kind of API.
And most of those API's are accessible with C.
Therefore, Python offers ctypes.
That's the lowest common denominator. Other API's are available, but you have to use Google to find them. Sometimes they're on PyPi. It's best to be specific and search for the specific app because there may be app-specific Python bindings. You don't know until you search.
MacOSX offers more sophisticated tools than this (via AppleScript and AppScript) but it's unique to Mac OS X.
Here's an example in Windows:
Utilizing os.system will work to ping (a native command in Windows):
import os
os.system('ping google.com')
Alternatively, you can also you the recommended subprocess.Popen:
import subprocess
subprocess.Popen('echo "hello world"', shell=True)
Related
I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.
Fact to Accept: Cross-Platform Printing is hard.
Depending on the system you use, the commands to send text / files to a printer will be very different.
For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc.
I honestly think that trying to solve it any other way would be a lot more complicated in the end.
For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.
Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.
The basic approach
You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need.
I don't know of any way around this.
Using the Python win32 modules needn't be as complex as you might think.
For Windows
This can be done with the module win32print,
Documented nicely here
For Linux, macOS, Unix
Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module
I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.
Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.
I want to develop a small Python app that interacts with the user via the console/command line. Are there any good libraries I can use to get user input and display the output in a nice-looking way? I tried searching but most of what I found was command-line argument processing, didn't see anything about user interaction in the shell.
It should be crossplatform (Windows and Linux)
A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
provides list of supported commands(they end up being implemented as methods)
help command that can be called to explain one of your commands
handles the whole entering a command, checking syntax, and calling your method command loop cycle.
users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.
You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.
If you need more, take a look at Urwid as well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer. :-)
Curses is the most widely used in the Unix environment according to the doc. For Windows you could look at Windows Console Driver, WConio - Windows CONsole I/O for Python or Wcurses. I couldn't find much on cross platform console libraries unfortuntaly.
If your Windows users are CLI users they probably have cygwin which has ncurse support so curses is still the best option if you ask me.
I have dowloaded a python script that uses win32com module (project site) to start and control Google Earth aplication on Windows. The commands in the script can also be used to interactively control Google Earth from command line.
The code is here. It works, it is exciting, BUT...
I'd like to do the same ON LINUX (Ubuntu derivatives).
The problem is: this script uses the COM interface to pass commands to GoogleEarth via interprocess communication. On linux, instead of an .exe, there is googleearth-bin executable, and I don't know how it would or shoud be to use the same interprocess communication idea on Linux.
Since the program is most probably compiled to the different OSes from (mostly) the same source code, it "seems" to me that the functionality could be in there. Now about HOW to access it from a Python script, this goes way beyond my current knowledge.
Thanks for any help!
The Google Earth COM Api has been depreciated (see http://googlegeodevelopers.blogspot.co.uk/2010/08/sunset-for-google-earth-com-api.html) so writing code against it is going to be hard-work and there are no guarantees that your code will work in the future.
Google are suggesting that people use the Plugin version of Google Earth but unfortunately, to date, this isn't supported on Linux OSs so that is not an option for you in this case.
Anyhow, caveates aside, you could look at something like pygoogleearth
http://pypi.python.org/pypi/pygoogleearth/
This is a pythonic wrapper around the Google Earth COM Interface, it should allow you to work with the COM api in python or at the least give you some pointers on the set-up.
I was wondering if there is any way to export python functions to dll. There is py2exe and I can successfully create exe file. My program should be used by another program written in delphi (there is possibility of importing dll's in delphi).
So I was wondering what would be the best way to connect those 2 applications.
Now I can only create exe, execute process in delphi and communicate in some way. But I don't think that's nice way. Maybe somebody have any experience in this subject?
There are some pretty big challenges to making languages work well together. As a simple alternative to trying to hook python code directly into delphi, you could consider using something like an xmlrpc server to provide python functionality remotely.
http://docs.python.org/library/xmlrpclib.html
Of course, any protocol could be used; xmlrpc just has some useful server utilities in python and presumably has a client library in delphi.
You can re-use python functions via modules. Integrating with other language is a different task all together.
py2exe packs all dependent modules and additional dlls required by an application so that it can be easily distributed without creating any installation dependencies for the user.
Cross - language integration requires some work. To integrate with "C", there are various ways like cython etc. If there is similar facility available with delphi, you might be able to use it.
Check out some of these references, it will make it more clear to you on what direction to take.
http://wiki.python.org/moin/IntegratingPythonWithOtherLanguages
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=python+delphi (Google search)
I'd like to write some quick Python code to assess the CPU, memory, disk, and networking usage of my Windows XP system.
Are there existing Python libraries that would allow me to access that information? Or, are there DLL's that I can call from Python? (If so, a code sample would be appreciated)
I think WMI is the resource to use. Especially, look at the Win32_PerfFormattedData* classes in the MSDN.
A quick search turned this up (among others):
http://timgolden.me.uk/python/wmi.html
The MS Scriptomatic tool can generate WMI scripts in Python as well as VBScript, JScript and Perl.