[英]Opening word document with read mode using python
I have a python applicaiton that need to luanch a word document . 我有一个python应用程序需要luanch一个word文档。 is there any option to luanch a word document with read mode only from python ? 是否有任何选项只能从python中读取具有读取模式的word文档?
You will find some very useful samples on the following page: 您将在下一页找到一些非常有用的示例:
Python for Windows: Microsoft Office 适用于Windows的Python: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. 打开一个Word文档只读就可以实现这个样子, True
为第三个参数Application.Documents.Open
告诉Word中打开该文档为只读。
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) 您始终可以通过命令从命令行启动msword(检查路径)
C:\Program Files\Microsoft Office\Office\Winword.exe /f <filename>
I am assuming you want to launch msword and not read word docs programmatically. 我假设你想要启动msword而不是以编程方式阅读word docs。 To be able to do that from python, you need to use the facility to run external commands. 为了能够从python中执行此操作,您需要使用该工具来运行外部命令。
see : http://docs.python.org/library/os.html#os.system 请参阅: 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. 我同意@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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.