今天就跟大家聊聊有關(guān)python中怎么處理document文檔,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

具體內(nèi)容如下
# -*- coding: utf-8 -*-
# 利用python-docx替換文章中的內(nèi)容
pip install python-docx
# 格式、線段、圖片、頁眉頁腳等都不變
# python-docx 在處理超鏈接的問題時,可以參考一下鏈接對源碼進(jìn)行修改
https://github.com/python-openxml/python-docx/issues/85
# 具體修改操作如下
\site-packages\docx\oxml\__init__.py
# 需要新增的代碼
def remove_hyperlink_tags(xml):
import re
text = xml.decode('utf-8')
text = text.replace("</w:hyperlink>","")
text = re.sub('<w:hyperlink[^>]*>', "", text)
return text.encode('utf-8')
# 需要修改的源碼
def parse_xml(xml):
root_element = etree.fromstring(remove_hyperlink_tags(xml), oxml_parser)
return root_element
"""
import os
from docx import Document
from win32com import client
# 自己寫的逐句翻譯包
import doc_scan
def pre_document(filename):
"""
由于python_docx(只能讀取.docx文件,不能讀取.doc文件)
將對應(yīng)文件夾下的doc文件轉(zhuǎn)為docx文件
:param filename: 文件的絕對路徑
:return:
"""
file_tuple = os.path.splitext(filename)
if file_tuple[1] == '.doc':
word = client.Dispatch('Word.Application')
doc = word.Documents.Open(filename) # 目標(biāo)路徑下的文件
doc.SaveAs(file_tuple[0] + ".docx", 16) # 轉(zhuǎn)化后路徑下的文件
doc.Close()
word.Quit()
# 把源文件刪除
os.remove(filename)
def read_document():
"""
原文文章為中文,然后將中文逐句翻譯成英文,把英文替換原來的中文,保留文章的原樣式
:return:
"""
# 遍歷doc文件下的所有的文件
path = os.path.dirname(os.path.abspath(__file__)) + '\doc'
for f in os.listdir(path):
file = "%s\%s" % (path, f)
# 對源文件進(jìn)行預(yù)處理
pre_document(file)
document = Document(file)
for num, paragraph in enumerate(document.paragraphs):
# 獲取每段中的文字
old_text = paragraph.text.strip()
if old_text:
inlines = paragraph.runs
if inlines:
# 將原有的文章里面的內(nèi)容為空
for li, inli in enumerate(inlines):
inlines[li].text = inlines[li].text.replace(inlines[li].text, '')
new_text = doc_scan.Scan(old_text)
# 把翻譯好的文章句子 替換到 零號位置上面
inlines[0].text = new_text
# 保存文件,覆蓋操作
document.save(file)
# 將document中的圖片下載到本地
# document = Document(file)
# for shape in document.inline_shapes:
# contentID = shape._inline.graphic.graphicData.pic.blipFill.blip.embed
# contentType = document.part.related_parts[contentID].content_type
# if not contentType.startswith('image'):
# continue
# imgName = basename(document.part.related_parts[contentID].partname)
# imgData = document.part.related_parts[contentID]._blob
# with open(imgName,'wb') as fp:
# fp.write(imgData)
if __name__ == '__main__':
read_document()看完上述內(nèi)容,你們對python中怎么處理document文檔有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標(biāo)題:python中怎么處理document文檔-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.chinadenli.net/article2/deisic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、服務(wù)器托管、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、電子商務(wù)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)