0

How and is it possible to open a document in a GUI text editor such as Word from a python script on a Mac? For example, do something like this:

Open x.txt in Word.app

Jonah Fleming
  • 1,149
  • 2
  • 16
  • 28

1 Answers1

0

Here is how you can do it on Mac OS:

If you want to open a file with a specific application on Mac OS, you do:

$ open -a appName fileName

So, for example you want to open hello.docx with Pages app, you do:

$ open -a pages path/hello.docx

Similarly to open a file with Word:

$ open -a "Microsoft Word.app" path/hello.docx

How you do it from Python (still only Mac OS):

Now, we need to be able to call shell commands from Python. There are several ways of doing it. You can see this question for a variety of ways.

One simple way of doing it would be:

>>> import os
>>> os.system('open -a "Microsoft Word.app" path/hello.docx')
Community
  • 1
  • 1
Sait
  • 16,365
  • 16
  • 65
  • 96