简体   繁体   English

使用python打开带有读取模式的word文档

[英]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.

相关问题 当我只能以只读模式打开某些文件时,使用 Python comtypes 打开和保存 word 文件? - Opening and Saving word files using Python comtypes when i can only open some files in read only mode? 使用Python打开Word文档时没有类型关系 - No relationship of type when opening Word document with Python Python:按顺序读取Word文档 - Python: Read Word Document In Order 使用python将数据从csv文件读取到word文档作为表格 - read data from csv file to word document as a table using python 通过使用 Python 在管理员模式下打开来自动化应用程序 - Automate an application by opening in Admin mode using Python 使用python打开chrome中的.pdf文档 - opening a .pdf document in chrome using python 如何在使用 python 生成的 Word 文档中激活“跟踪更改”模式 - How to activate "Track changes"-Mode in a word document generated with python 以只读模式从python打开sqlite3数据库 - Opening sqlite3 database from python in read-only mode 如何使用 AWS Lambda Python 读取 AWS S3 存储的 word 文档(.doc 和 .docx)文件内容? - How to read AWS S3 stored word document (.doc and .docx) file content using AWS Lambda Python? 使用Python启动Excel应用程序以查看CSV文件,但CSV文件在读取模式下打开,无法查看写在其上的数据 - launching Excel application using Python to view the CSV file , but CSV file is opening in read mode and cant view the data written on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM