Forum Archive

Opening Word documents in Pythonista

Kenbo01

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:// ' ) 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?

Thanks

Ken

the_buch

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)

console.quicklook(outfile_path)

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

Probably the webbrowser module, or console.quicklook() can open word documents

http://omz-software.com/pythonista/docs/library/webbrowser.html

Kenbo01

Thanks. I wasn't aware of console.quicklook() 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 console.quicklook() the results are mixed:

1) 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.

2) 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.

Thanks

the_buch

I had that problem at first, the Open In wouldn't work. I think it was the os.path part was what fixed that, if I recall.

Kenbo01

Thank you!! Yes, os.path.abspath 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.

the_buch

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!

Kenbo01

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...

Any ideas?

ccc

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?

Kenbo01

:) I better set one of those up.

For now, here's a code snippet demonstrating the issue:


from bottle import get, post, request, run

import webbrowser

import console

import os

@get('/start')

def start():

f = 'test.docx'

outfile_path = os.path.abspath(f)

console.quicklook(outfile_path)

return 'Success!'

webbrowser.open('http://localhost:8080/start')

run(host='localhost', port=8080)

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

Kenbo01

I've got a solution that works. I've replaced console.quicklook(outfile_path) with webbrowser.open('file://'+quote(outfile_path)).

Should have thought of that before!