导航:首页 > 编程语言 > pythonword解析

pythonword解析

发布时间:2022-08-22 21:57:52

① 如何用python写word口令破解器

python支持使用com技术调用word,但是不能直接操纵word文件,因为word文件是私有格式。所以你说的正则表达式查找替换,如果word本身不支持,那就没有办法了

② python word文件处理

#-*- encoding: utf8 -*-
import win32com
from win32com.client import Dispatch, constants
import win32com.client
import __main__
import os
import new
import sys
import re
import string
reload(sys)
sys.setdefaultencoding('utf8')
#from fileinput import filename

class Word(object):
#初始化word对象
def __init__(self, uri):
self.objectword(uri)

#创建word对象
def objectword(self,url):
self.word = win32com.client.Dispatch('Word.Application')
self.word.Visible = 0
self.word.DisplayAlerts = 0

self.docx = self.word.Documents.Open(url)
self.wrange = self.docx.Range(0, 0)

#关闭word
def close(self):
self.word.Documents.Close()
self.word.Quit()
#创建word
def create(self):

pass
#在word中进行查找
def findword(self, key):
question = []
uri = r'E:\XE\ctb.docx'
self.objectword(uri)
#读取所有的word文档内容
range = self.docx.Range(self.docx.Content.Start,self.docx.Content.End)
question = str(range).split("&")
#查找内容
#question = re.split(r"(\r[1][0-9][0-9]+.)",str(range))
#l = question[0].split("\d+.")
for questionLine in question:
questionLine = questionLine.strip('\n')
l = re.split(r"([1][0-9][0-9]+.)",questionLine)
del l[0]
for t in l:
s = str(key[0:3])
if str(t).find(s) > -1:
#插入
g = string.join(l)

print g.encode('gb2312')
#print g.decode("")
self.insertword(g)
print "sss"
else:
print "ttt"

#插入word
def insertword(self,w):
url = r'E:\XE\ctb.doc'
self.objectword(url)
self.wrange.InsertAfter(w)
pass

#读取数据源
def source(self, src):
f = open(src)
d = f.readlines()
for l in d:
name, question01, question02, question03, question04, question05 = tuple(l.decode('utf8').split('\t'))
if question01 != u'全对':
#self.wrange.InsertAfter(name)
self.findword(question01)
return self

Word(r'E:\XE\xx.docx').source(r'E:\XE\xe.txt').close()

③ 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()

④ 如何在 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,网上看了些代码,但自己的老是报错,求高手看看!!!

看了一下应该是没有自动创建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文件中某个关键字之后的表格

最好是全部都读取到程序中,在程序中进行判断。

本文实例讲述了Python实现批量读取word中表格信息的方法。分享给大家供大家参考。具体如下:
单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个python小程序把所需的信息打印出来
#coding:utf-8
import os
import win32com
from win32com.client import Dispatch, constants
from docx import Document
def parse_doc(f):
"""读取doc,返回姓名和行业
"""
doc = w.Documents.Open( FileName = f )
t = doc.Tables[0] # 根据文件中的图表选择信息
name = t.Rows[0].Cells[1].Range.Text
situation = t.Rows[0].Cells[5].Range.Text
people = t.Rows[1].Cells[1].Range.Text
title = t.Rows[1].Cells[3].Range.Text
print name, situation, people,title
doc.Close()
def parse_docx(f):
"""读取docx,返回姓名和行业
"""
d = Document(f)
t = d.tables[0]
name = t.cell(0,1).text
situation = t.cell(0,8).text
people = t.cell(1,2).text
title = t.cell(1,8).text
print name, situation, people,title
if __name__ == "__main__":
w = win32com.client.Dispatch('Word.Application')
# 遍历文件
PATH = "H:\work\\aaa" # windows文件路径
doc_files = os.listdir(PATH)
for doc in doc_files:
if os.path.splitext(doc)[1] == '.docx':
try:
parse_docx(PATH+'\\'+doc)
except Exception as e:
print e
elif os.path.splitext(doc)[1] == '.doc':
try:
parse_doc(PATH+'\\'+doc)
except Exception as e:
print e

希望本文所述对大家的Python程序设计有所帮助。

⑦ python读取已经打开的3个word和excle文件的路径

使用os.path.abspath()函数来获取文件绝对路径

文件目录结构如下:

⑧ 如何在mac上用python批量将word文件转成txt文件 / 网络技术编程

python实现起来可能没有现成解决方案。因为py库可能没有全面的office套件解析器。

mac想批量转,很容易。

  1. 安装openoffice。

  2. 终端执行 soffice --headless --convert-to txt my_file.doc/.docx

    如果批量将当前目录下所有doc转为txt,则写过简单shell:

    for i in `ls *doc`; do soffice --headless --convert-to txt $i ; done; 即可。

  3. 以上同时适用linux。

⑨ 求助大神:如何用Python docx解析一个Word文档,在某些字段处插入文本或表格,更换页眉页脚等急~

from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='IntenseQuote')

document.add_paragraph(
'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
'first item in ordered list', style='ListNumber'
)

document.add_picture('monty-truth.png', width=Inches(1.25))

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for item in recordset:
row_cells = table.add_row().cells
row_cells[0].text = str(item.qty)
row_cells[1].text = str(item.id)
row_cells[2].text = item.desc

document.add_page_break()

document.save('demo.docx')
这是一个demo for docx 你可以试试

⑩ python处理word文档

有个库叫‘Python-docx’
安装之后 python 可以读写 word 文档,就可以拼接了。

阅读全文

与pythonword解析相关的资料

热点内容
修改本地账户管理员文件夹 浏览:416
python爬虫工程师招聘 浏览:283
小鹏p7听音乐哪个app好 浏览:354
linux下的防火墙 浏览:954
凌达压缩机美芝压缩机 浏览:350
php后面代码不执行 浏览:236
微我手机怎样设置应用加密 浏览:202
条件加密 浏览:628
androidstudio设置中文 浏览:641
汽车换压缩机能提升制冷 浏览:628
安卓开发配什么电脑 浏览:607
linux下php模块 浏览:78
阿里云服务器终端在哪里 浏览:147
app纸有什么用 浏览:224
cuteftp命令 浏览:507
最开始的编程语言是什么 浏览:759
at远程命令 浏览:492
云服务器哪家好点 浏览:215
android系统源码阅读 浏览:931
dumpjava分析工具 浏览:680