导航:首页 > 编程语言 > Python无法读取word修订内容

Python无法读取word修订内容

发布时间:2022-06-24 10:20:32

python 不能read word文件

WORD文件有专门的格式,你当然不可能像文本文件一样读写,你这样打开只能看到一堆乱码。
Python本身没有这个功能,修改WORD文件的话Windows有专门的API,在Office开发套件里面,你用Python调用相关的库才能实现你想要的功能。

❷ python如何读取word文件

>>>defPrintAllParagraphs(doc):
count=doc.Paragraphs.Count
foriinrange(count-1,-1,-1):
pr=doc.Paragraphs[i].Range
printpr.Text


>>>app=my.Office.Word.GetInstance()
>>>doc=app.Documents[0]
>>>PrintAllParagraphs(doc)

1.什么是域

域应用基础

>>>
@staticmethod
defGetInstance():
u'''获取Word应用程序的Application对象'''
importwin32com.client
returnwin32com.client.Dispatch('Word.Application')
  1. my.Office.Word.GetInstance的方法实现如上,是一个使用win32com操纵Word Com的接口的封装

  2. 所有Paragraph即段落对象,都是通过Paragraph.Range.Text来访问它的文字的

❸ 我想用Python操作word,网上看了些代码,但自己的老是报错,求高手看看!!!

看了一下应该是没有自动创建constants变量,constants是空的

先运行语句:
win32com.client.gencache.EnsureDispatch('Word.Application')
应该就可以了

或者运行pythonwin菜单栏选择Tools——>Com MakePy Utility然后在弹出的窗口中选择Microsoft Word x.y Object Library 点击OK就可以了
或者直接运行client文件夹下的makepy.py文件同样选择Microsoft Word 也可以

❹ python如何读取word文件中的文本内容并写入到新的txt文件

❺ python读取word文档内容

import fnmatch, os, sys, win32com.client

readpath=r'D:\123'

wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
try:
for path, dirs, files in os.walk(readpath):
for filename in files:
if not fnmatch.fnmatch(filename, '*.docx'):continue
doc = os.path.abspath(os.path.join(path,filename))
print 'processing %s...' % doc
wordapp.Documents.Open(doc)
docastext = doc[:-4] + 'txt'
wordapp.ActiveDocument.SaveAs(docastext,FileFormat=win32com.client.constants.wdFormatText)
wordapp.ActiveDocument.Close()
finally:
wordapp.Quit()
print 'end'

f=open(r'd:\123\test.txt','r')
for line in f.readlines():
print line.decode('gbk')
f.close()

❻ python生成的程序无法操作Word

word中doc这个格式的文件是微软特有格式,微软没有向外公开任何的api接口文档,
只能通过微软提供的OLE组件来提其COM接口,只要你的机器上安装了Offices完整的办公软件,在安装目录下面有个MSWORD.OLB组件,导入这个即可。

1、相关操作Excel有MSPPT.OLB,EXCEL.exe等。

❼ 如何在 Linux 上使用 Python 读取 word 文件信息

第一步:获取doc文件的xml组成文件

import zipfiledef get_word_xml(docx_filename):
with open(docx_filename) as f:
zip = zipfile.ZipFile(f)
xml_content = zip.read('word/document.xml')
return xml_content

第二步:解析xml为树形数据结构
from lxml import etreedef get_xml_tree(xml_string):
return etree.fromstring(xml_string)

第三步:读取word内容:
def _itertext(self, my_etree):
"""Iterator to go through xml tree's text nodes"""
for node in my_etree.iter(tag=etree.Element):
if self._check_element_is(node, 't'):
yield (node, node.text)def _check_element_is(self, element, type_char):
word_schema = '99999'
return element.tag == '{%s}%s' % (word_schema,type_char)

❽ 如何用python读取word

使用Python的内部方法open()读取文本文件

try:
f=open('/file','r')
print(f.read())
finally:
iff:
f.close()

如果读取word文档推荐使用第三方插件,python-docx 可以在官网上下载

使用方式

#-*-coding:cp936-*-
importdocx
document=docx.Document(文件路径)
docText=' '.join([
paragraph.text.encode('utf-8')forparagraphindocument.paragraphs
])
printdocText

❾ python能打开word文档吗

首先下载安装win32com

from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open('c:/test')
doc.SaveAs('c:/test.text', 2)
doc.Close()
word.Quit()

这种方式产生的text文档,不能用python用普通的r方式读取,为了让python可以用r方式读取,应当写成

doc.SaveAs('c:/test', 4)

注意:系统执行完成后,会自动产生文件后缀txt(虽然没有指明后缀)。
在xp系统下面,应当,

open(r'c:\text','r')
wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
= 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
= 13
wdFormatXMLTemplate = 14
= 15
wdFormatXPS = 18

照着字面意思应该能对应到相应的文件格式,如果你是office
2003可能支持不了这么多格式。word文件转html有两种格式可选wdFormatHTML、wdFormatFilteredHTML(对应数字
8、10),区别是如果是wdFormatHTML格式的话,word文件里面的公式等ole对象将会存储成wmf格式,而选用
wdFormatFilteredHTML的话公式图片将存储为gif格式,而且目测可以看出用wdFormatFilteredHTML生成的HTML
明显比wdFormatHTML要干净许多。

当然你也可以用任意一种语言通过com来调用office API,比如PHP.

from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open(r'c:/test1.doc')
doc.SaveAs('c:/test1.text', 4)
doc.Close()

import re
strings=open(r'c:\test1.text','r').read()
result=re.findall('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)',strings)
chan=re.sub('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)','()',strings)
question=open(r'c:\question','a+')
question.write(chan)
question.close()
answer=open(r'c:\answeronly','a+')
for i,a in enumerate(result):
m=re.search('[A-D]',a)
answer.write(str(i+1)+' '+m.group()+'\n')
answer.close()
chan=re.sub(r'\xa3\xa8\s*[A-D]\s*\xa3\xa9','()',strings)
#不要(),容易引起歧义。

阅读全文

与Python无法读取word修订内容相关的资料

热点内容
专业安全文件夹 浏览:777
表格里的根号算法怎么打 浏览:193
javacorepdf 浏览:573
pdf转换word编辑 浏览:446
35岁程序员实习期恐慌 浏览:701
如何做一个系统u盘文件夹名字 浏览:968
如何确认哪个ip重启了服务器 浏览:130
照片压缩软件绿色版 浏览:109
pgp基于什么体系加密 浏览:637
python合法赋值语句格式 浏览:713
程序员数学线性代数 浏览:624
看帧率app如何使用 浏览:525
从DHC服务器租用IP地址 浏览:477
编译怎么学 浏览:333
数码管显示0到9plc编程 浏览:667
服务器是为什么服务的 浏览:769
java定义数据类型 浏览:878
安卓pdf手写 浏览:431
什么是app开发者 浏览:288
android闹钟重启 浏览:105