is it possible to read data from an OPEN Libreoffice Calc file? - python

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.

Related

How to edit text file through iPython Notebook connected to remote server?

I'm exploring a new workflow for interacting with a remote server and iPython has many attractive properties for session logging and scripting. The one thing I have not figured out yet is how to do effective text file manipulation on the remote machine.
I'm aware of the %load and %writefile magic commands, but that's quite awkward for general use.
Is there a way to get %edit to work when using Notebook connected to a remote (*nix) server? Best solution would be an simple editor that opens up in the web browser similar to the panes created by the command_name? help viewer. My search seems to suggest that doesn't exist. I have found this PEP requesting it.
I figure it's probably possible to have %edit trigger an X-windows editor to open up on my local machine, but I don't see how to make that happen. That would be a fine answer if there's a way to do it.
I should also clarify that I'm not generally interested in executing python code in the files, I just want to edit the file. i.e. %edit -x would probably be my general use case.

python copying directory and reading text files Remotely

I'm about to start working on a project where a Python script is able to remote into a Windows Server and read a bunch of text files in a certain directory. I was planning on using a module called WMI as that is the only way I have been able to successfully remotely access a windows server using Python, But upon further research I'm not sure i am going to be using this module.
The only problem is that, these text files are constantly updating about every 2 seconds and I'm afraid that the script will crash if it comes into an MutEx error where it tries to open the file while it is being rewritten. The only thing I can think of is creating a new directory, copying all the files (via script) into this directory in the state that they are in and reading them from there; and just constantly overwriting these ones with the new ones once it finishes checking all of the old ones. Unfortunately I don't know how to execute this correctly, or efficiently.
How can I go about doing this? Which python module would be best for this execution?
There is Windows support in Ansible these days. It uses winrm. There are plenty of Python libraries that utilize winrm, just google it, but Ansible is very versatile.
http://docs.ansible.com/intro_windows.html
https://msdn.microsoft.com/en-us/library/aa384426%28v=vs.85%29.aspx
I've done some work with WMI before (though not from Python) and I would not try to use it for a project like this. As you said WMI tends to be obscure and my experience says such things are hard to support long-term.
I would either work at the Windows API level, or possibly design a service that performs the desired actions access this service as needed. Of course, you will need to install this service on each machine you need to control. Both approaches have merit. The WinAPI approach pretty much guarantees you don't invent any new security holes and is simpler initially. The service approach should make the application faster and required less network traffic. I am sure you can think of others easily.
You still have to have the necessary permissions, network ports, etc. regardless of the approach. E.g., WMI is usually blocked by firewalls and you still run as some NT process.
Sorry, not really an answer as such -- meant as a long comment.
ADDED
Re: API programming, though you have no Windows API experience, I expect you find it familiar for tasks such as you describe, i.e., reading and writing files, scanning directories are nothing unique to Windows. You only need to learn about the parts of the API that interest you.
Once you create the appropriate security contexts and start your client process, there is nothing service-oriented in the, i.e., your can simply open and close files, etc., ignoring that fact that the files are remote, other than server name being included in the UNC name of the file/folder location.

How to control an outside executable application?

I have my own application on python 2.7. I want to control an outside .exe application.
I'm able to launch such an application, klm.exe as:
from win32com.client import *
ExtApp = Dispatch("Wscript.Shell")
ExtApp.Run("E:\XYZ\ABC\klm")
But I want to have full control of this outside .exe application as it has tabs, radio buttons, push buttons, etc.
Is there a way to do so?
But I want to have full control of this outside .exe application as it has tabs, radio buttons, push buttons, etc.
Is there a way to do so?
Yes, multiple ways, depending on the application.
Since you're already using COM (although I'm not sure why you're using it just to launch apps)… does the app have a COM automation (IDispatch) interface? If so, there will probably be documentation showing how to use it from VB# (or VBScript or C# or …), which you can easily adapt to Python and win32com. (For an example of such an application, see the Outlook automation docs.)
If there's no COM automation interface, there may still be a lower-level COM interface, which is almost as easy to use via win32com, but it usually won't provide any access to the GUI controls; instead, you'll be talking to the same lower-level functionality that the GUI uses. (For a good example, see Apple's iTunes COM Interface.)
If there's no COM support at all, the simplest thing to do is to automate it via Windows WM_* events. There are some examples of doing that in the pywin32 documentation, but there are also a lot of higher-level wrappers, like AutoPy and pywinauto/swapy, and so on that will make things a whole lot easier. There are dozens of these, free and commercial, and even more if you're willing to step outside of Python and use a different scripting system, and SO is not a good place to discuss the pros and cons of each.
Finally, you can always ignore the app's windows and just automate the system mouse… but this is almost always a silly thing to do.

Is there an easy way to distribute a Flask server as an executable?

I'm building some simple editors with Backbone.js, and I'm hoping to be able to distribute them as apps for users to edit content in a mostly client-side way (i.e., I don't want users to have to futz with setting up stuff like MySQL or Apache).
So I was imagining a scenario like:
User downloads a .zip file
In the resulting opened folder, the user clicks index.html
That opens in a browser
Backbone app starts, stores data in localStorage
The user can then export to CSV.
Believe it or not, that would solve my problem: I want to help users edit data in a browser and then get it back out in a familiar format (CSV can be loaded into Excel, for instance).
And I’d like to do this without forcing them to configure a server. It seems like this is almost possible in the HTML5 stack. However, in at least one browser (Chrome), this doesn't work, because I get errors like this one:
XMLHttpRequest cannot load file:///users/me/project/data/Appdata.json. Origin null is not allowed by Access-Control-Allow-Origin.
(Oddly enough, I don't get that error in Firefox, and the .js or .json files load fine.)
So at this point it seems to me that there's no way around having these users use something kind of local server to serve up the Backbone interface.
So, I'm trying to figure out how to build a distributable, cross-platform executable that will allow my users to start a Flask server. (I hope to build a REST backend to a Backbone.js app.)
Is this wishful thinking? I'm assuming I can get the people in question to install Python.
Is this doable? There seem to be many ways to package up Python programs, (pyinstaller? py2exe? ...) So I thought I would ask here in case someone might know of a solution for the stack I have in mind.
TIA!
You can use Anthony Gordon McMillan’s Pyinstaller or Tuininga’s cx_Freeze
Quoting the PyInstaller website:
Features
Packaging of Python programs into standard executables, that work on
computers without Python installed.
Multiplatform: works under
Windows (32-bit and 64-bit),
Linux (32-bit and 64-bit),
Mac OS X (32-bit only, 64-bit in git, see Features/MacOsCompatibility) and
experimentally Solaris and AIX (in git).
Multiversion: works under any version of Python from 2.2 up to 2.7.
My suggestion would be to create a thin service wrapper around your code. This will allow the server to run independently of your main codebase - also allowing the user to shut down the server directly (simply right clicking the service icon and selecting "Exit").
This SO answer should help you get started.
After reading your updated question, I think something like mongoose might be more suited to your task. It is an embeddable web server that is FLOSS and has python bindings. Flask might be overkill.
Not easily. On Windows, you'd have to include Python itself. Mac and Linux usually have Python installed, but you can't be sure of what version so it's often easier to bundle your specific Python for them as well. Then you'd have to include all the dependencies that you want to run with in your package or be able to install them with pip, easy_install, etc.
You can use py2app and py2exe. This won't be cross-platform as you'll still need to make a different version for each target OS. The only way to make it cross-platform is to bundle all versions and have some cross-platform code execute the appropriate version for that platform.
If you need databases like MySQL or even SQLite things get more complicated as you'll have to include those too.

How to remotely update Python applications

What is the best method to push changes to a program written in Python? I have a piece of software that is written in Python that will regularly be updated. What would be the best way to do this? All the machines will have Windows 7.
Also, excuse the ambiguity of my question. This will be my first time having to implement an updating procedure. Feel free to mention specifics you would like me ot add.
If you're not already packaging your program with InnoSetup, I strongly recommend you switch to it, because it has facilities to make this sort of thing easier. You can specify any special situations, such as files that should not be updated if they already exist (i.e. if you have any internal configuration files or things like that), in the InnoSetup script.
Next, to allow the client machine to find out about new versions of your app, keep a very small file on your public web server that has the version number of the current release and the URL to the latest version's installer exe. For this file to be useful, whenever you release a newer version of your program you must update this file, as well as the version number in the InnoSetup script, and also some kind of APP_VERSION constant in your program.
Then, you'll need to handle these parts of the updater yourself:
Detecting when a newer version is available by retrieving the current-version file from your web server over HTTP, and comparing the version number there to the app's own APP_VERSION. Make sure to do this query in a way that fails gracefully if the client machine doesn't have Internet access, and that doesn't block the GUI while it is doing the request (in case there's a network issue that forces the query to wait a long while for a timeout).
If a newer version is available, asking the user if they want to update, and if they say yes downloading an updated installer to the TEMP directory. Depending on what GUI toolkit you are using, there are various mechanisms for displaying a progress dialog during the download; this is a good idea since the installer is likely to be at least an MB.
Closing your app, running a special update script in the background, then starting up the app again.
The update script will wait for the original process to die completely (easiest way to do this is to pass in the original process's PID as a command line argument and have the update script send a query signal 0 to that process every second or so until it goes away.) It can then run the installer silently in the background, perhaps while displaying a "Please Wait..." dialog to the user. Once the installer is done and reports success in its return code, the updater can restart your program.
Depending on how big your app is, this is more wasteful of bandwidth than the method using git or another SCM. Every update with this approach would involve downloading the entire installer for the latest version of the app, whereas an SCM would only download the files that have changed. However, it has the advantage that it requires no special server facilities except a regular web server, and no special installation of the SCM client on the user's computer.
Plus, InnoSetup is just generally cool. :-)
I would suggest using a source control program such as git or subversion. Also, if you are okay with everyone seeing the code, you can post the code on github, where anyone can pull from it. You could make it private, but you would have to pay for it and all the users would also have create a github account and set it up with their git install.
If you use a source control program, the other people will have to pull the edits manually by running a command, but you could make a script pr batch file that does this and have it run at start up or at regular intervals.
Just to be clear, if you want to do this, you will have to put the code on a server with and SSH support and set up git. If you don't want to go through all of the server set up, I would reccomend github.
git- http://git-scm.com/ (For windows version, go to downloads and select msysGit)
github - https://github.com/
For those of you that would be looking into something a little less dated, I was just looking at how to create python applications that can be updated remotely (though not limited to Windows like OP).
It seems like esky as been a solution for a while. Though it's been deprecated since 2018.
The latest and most up to date solution seem to be a combination of pyinstaller and pyupdater. Note that I don't have personal experience with it, I'm looking for a friend.
It seems to support windows, linux and Mac though and both python 2 and 3 so definitely worth having a look.
The basic principles of application updates are described well by DSimon's answer.
However, update security is a different matter altogether: You don't want your clients to end up installing malicious files.
PyUpdater, as suggested in jlengrand's answer, does provide some secure update functionality, but, unfortunately, PyUpdater 4.0 is broken and there has not been a new release in over half a year (now Aug 2022).
There's also python-tuf, which is the reference implementation of The Update Framework (TUF).
TUF (python-tuf) does everything humanly possible to ensure your update files are distributed securely. However, it does not handle application-specific things like checking for new application versions and installation on the client side.

Categories