简体   繁体   中英

Opening word document with read mode using python

I have a python applicaiton that need to luanch a word document . is there any option to luanch a word document with read mode only from python ?

You will find some very useful samples on the following page:

Python for Windows: Microsoft Office

Opening a Word document read-only can be achieved like this, True as the third parameter to Application.Documents.Open tells Word to open the document read-only.

import win32com.client, pythoncom, time

def word(wordfile):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
    myWord = win32com.client.DispatchEx('Word.Application')
    myDoc = myWord.Documents.Open(wordfile, False, False, True)

    ...

    myDoc.Close()
    myWord.Quit()
    del myDoc
    del myWord
    pythoncom.CoUninitialize()

You could always fire up the msword from command line via the command (Check the path)

C:\Program Files\Microsoft Office\Office\Winword.exe /f <filename>

I am assuming you want to launch msword and not read word docs programmatically. To be able to do that from python, you need to use the facility to run external commands.

see : http://docs.python.org/library/os.html#os.system

import os
os.system(command)

or:

import os
import subprocess
subprocess.call(command)

See the various command line options at:

I agree with @pyfunc. Just a small suggestion. When you have spaces in paths , sometimes it doesn't work . So you need to mention it like this:

 C:\"Program Files (x86)"\"Microsoft Office 2013"\Office15\WINWORD.exe D:\inchowar\Desktop\Junk.docx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM