本文目录一览:
- 1、python读取word文档内容
- 2、python如何读取word文件
- 3、python如何读取word文件中的文本内容并写入到新的txt文件?
- 4、python抓取word保存excel
- 5、如何用python读取word
- 6、如何在 Linux 上使用 Python 读取 word 文件信息
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文件
def PrintAllParagraphs(doc):
count=doc.Paragraphs.Count
for i in range(count-1,-1,-1):
pr=doc.Paragraphs[i].Range
print pr.Text
app=my.Office.Word.GetInstance()
doc=app.Documents[0]
PrintAllParagraphs(doc)
1.什么是域
域应用基础
@staticmethod
def GetInstance():
u'''获取Word应用程序的Application对象'''
import win32com.client
return win32com.client.Dispatch('Word.Application')
my.Office.Word.GetInstance的方法实现如上,是一个使用win32com操纵Word Com的接口的封装
所有Paragraph即段落对象,都是通过Paragraph.Range.Text来访问它的文字的
python如何读取word文件中的文本内容并写入到新的txt文件?
#确保安装了python-docx包
from docx import Document as Doc
docu=Doc(input('path:'))
file=''
for i in docu.paragraphs:
----file+=i.text
f=open(input('new path:'),'w',encoding='utf-8')
f.write(file)
f.close()
#减号的位置是缩进
python抓取word保存excel
利用下面的方法把Word转成Excel,然后再进行编辑处理:
方法一:在word打开需要编辑的文档选中并复制需要转换的文档进入excel文档,然后选择一个单元格,点击右键,然后选择粘贴(这个方法比较简单,适用于一些规则的表格转换);
方法二:在word中打开需要转换的文档,并复制在excel选择一个单元格,然后右键,然后选择选择性粘贴弹出对话框,我们可以看到有几个选项确定好后,我们就可以看到,大功告成啦。
如何用python读取word
使用Python的内部方法open()读取文本文件
try:
f=open('/file','r')
print(f.read())
finally:
if f:
f.close()
如果读取word文档推荐使用第三方插件,python-docx 可以在官网上下载
使用方式
# -*- coding: cp936 -*-
import docx
document = docx.Document(文件路径)
docText = '/n/n'.join([
paragraph.text.encode('utf-8') for paragraph in document.paragraphs
])
print docText
如何在 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)