Opening Word documents in Pythonista
What's the easiest way to get a Pythonista script to display a Word document that I have stored in a Pythonista folder?
I can't seem to use webbrowser.open('pythonista:// <filename>' ) because it assumes I am trying to launch a script.
I know that Pythonista can open a Word file (presumably using Quick Look) because it does so when I simply launch the file from the Pythonista GUI.
Is my only option to use URL scheme to launch another app like Pages?
I have a script to convert markdown to a docx file. With the help of some great people at these forums I was able to get a preview in Pythonista of the final product from which I can then use the open in menu. Here's what that part of my code looks like:
outfile_path = os.path.abspath(outfile)
outfile being the final docx product. If you know Python better than I, you might be able to adapt that to your needs, but I was thinking if you pass the file as input one way or another, that ought to work. I believe Pages is available from the Open In menu there. I don't think Pages has a URL scheme. If I'm wrong though, I'd like to know.
jldiaz last edited by
console.quicklook()can open word documents
Thanks. I wasn't aware of <b>console.quicklook()</b> for some reason and it seems to be exactly what I am after.
However, I am getting inconsistent results.
I've tried three documents, one PDF and 2 .docx files.
They all show correctly if I launch then from the Pythonista GUI.
But if I use <b>console.quicklook()</b> the results are mixed:
the first PDF displays an empty screen with just "Portable Document Format" and file size. Open In appears, but it fails to send the PDF to any app except for Mail.
the 2 .docx files display OK, but Open In cannot seem to send to any app other than Mail.
In the process, I discovered that console.quicklook() won't raise an error for a non-existent file just displays a "Loading" screen.
In any event, the display of .docx will allow me to progress my app.
I had that problem at first, the Open In wouldn't work. I think it was the
os.pathpart was what fixed that, if I recall.
Thank you!! Yes, <code>os.path.abspath</code> fixed both the display of the PDF and the ability to Open In other applications. I should have paid closer attention to your first post.
You're welcome. And that's alright. I'm still quite new to Python, personally, and I only ever figured out that problem through the forum as well. Glad it's here. Also glad you got it working. Cheers!
So, onto the next related challenge.
My application is using the webbrowser and a local IPad web server to provide basic GUI elements.
However, I also need it display Word documents periodically.
I'm happy to do this by dropping to the console, but it doesn't seem to let me.
The very code above that successfully displays a Word doc when in a standalone script, won't work from within my code. I just get a spinning dial.
I'd be happy even to exit the webbrowser/web server but I don't know how...
ccc last edited by
It is easier to debug Python code than English prose. Would it be possible to post some code to GitHub for us to look at?
:) I better set one of those up.
For now, here's a code snippet demonstrating the issue:
from bottle import get, post, request, run
f = 'test.docx' outfile_path = os.path.abspath(f) console.quicklook(outfile_path) return 'Success!'
I realise that I'm using a web browser and web server and then calling a console routine, but I also need to be able to display Word documents even if it means completely dropping out of the webbrowser and terminating the web server.
Alternatively, could I force it to spawn a new thread for the console quicklook display, or is it doing that anyway?
Many thanks for any ideas
I've got a solution that works. I've replaced <code>console.quicklook(outfile_path)</code> with <code>webbrowser.open('file://'+quote(outfile_path))</code>.
Should have thought of that before!