python處理document文檔保留原樣式
更新時間:2019年09月23日 10:32:28 作者:Cocktail_py
這篇文章主要為大家詳細(xì)介紹了python處理document文檔保留原樣式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
document文檔格式、線段、圖片、頁眉頁腳等都不變,供大家參考,具體內(nèi)容如下
# -*- coding: utf-8 -*- # @Time : 2019/5/6 11:46 # @Author : """ # 利用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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)遠(yuǎn)程調(diào)用MetaSploit的方法
這篇文章主要介紹了Python實(shí)現(xiàn)遠(yuǎn)程調(diào)用MetaSploit的方法,是很有借鑒價值的一個技巧,需要的朋友可以參考下2014-08-08python圖的深度優(yōu)先和廣度優(yōu)先算法實(shí)例分析
這篇文章主要介紹了python圖的深度優(yōu)先和廣度優(yōu)先算法,結(jié)合實(shí)例形式分析了圖的深度優(yōu)先算法與廣度優(yōu)先算法相關(guān)概念、原理、實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-10-10python中websockets與主線程傳遞參數(shù)的實(shí)現(xiàn)
本文主要介紹了python中websockets與主線程傳遞參數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02