導航:首頁 > 編程語言 > 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解析相關的資料

熱點內容
百姓網app截圖是什麼意思 瀏覽:222
php如何嵌入html 瀏覽:809
解壓專家怎麼傳輸 瀏覽:743
如何共享伺服器的網路連接 瀏覽:132
程序員簡易表白代碼 瀏覽:166
什麼是無線加密狗 瀏覽:62
國家反詐中心app為什麼會彈出 瀏覽:67
cad壓縮圖列印 瀏覽:102
網頁打開速度與伺服器有什麼關系 瀏覽:863
android開發技術文檔 瀏覽:64
32單片機寫程序 瀏覽:49
三星雙清無命令 瀏覽:837
漢壽小程序源碼 瀏覽:343
易助erp雲伺服器 瀏覽:532
修改本地賬戶管理員文件夾 瀏覽:418
python爬蟲工程師招聘 瀏覽:285
小鵬p7聽音樂哪個app好 瀏覽:357
linux下的防火牆 瀏覽:964
凌達壓縮機美芝壓縮機 瀏覽:353
php後面代碼不執行 瀏覽:238