首页 > 解决方案 > 是否有任何方法或方法可以组合单个单词的运行,该单词已在 docx 文件中拆分为多个运行

问题描述

我有一个具有特定样式和结构的 docx 文件,我想用其他单词替换该 docx 文件的某些单词,但我不想修改 docx 文件的格式。

例如。如果我在 docx 文件中有以下提到的特定字体和大小的文本

这用于测试

我想用testing替换这个。我应该得到

这用于检查

字体样式和字号与测试字相同

为此,我正在尝试修改 docx 的 xml 文件。在检查 xml 文件时,注意到 docx 文件将段落文本分成多个运行。如下文所述,这是用于测试xml 文件给出的

<w:document
    xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
    xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
    xmlns:w10="urn:schemas-microsoft-com:office:word"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
    xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
    xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
    xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
    xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
    xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
    xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se wp14">\n  
    <w:body>\n    
        <w:p w:rsidR="00084F90" w:rsidRPr="00B6714F" w:rsidRDefault="00084F90" w:rsidP="00084F90">\n      
            <w:pPr>\n        
                <w:rPr>\n          
                    <w:sz w:val="40"/>\n          
                    <w:szCs w:val="40"/>\n        
                </w:rPr>\n      
            </w:pPr>\n      
            <w:r>\n        
                <w:rPr>\n          
                    <w:b/>\n          
                    <w:bCs/>\n          
                    <w:sz w:val="40"/>\n          
                    <w:szCs w:val="40"/>\n        
                </w:rPr>\n        
                <w:t>This is a dock</w:t>\n      
            </w:r>\n      
            <w:r w:rsidRPr="00B6714F">\n        
                <w:rPr>\n          
                    <w:b/>\n          
                    <w:bCs/>\n          
                    <w:sz w:val="40"/>\n          
                    <w:szCs w:val="40"/>\n        
                </w:rPr>\n        
                <w:t xml:space="preserve"> document</w:t>\n      
            </w:r>\n      
            <w:r w:rsidRPr="00B6714F">\n        
                <w:rPr>\n          
                    <w:sz w:val="40"/>\n          
                    <w:szCs w:val="40"/>\n        
                </w:rPr>\n        
                <w:t>.</w:t>\n      
            </w:r>\n    
        </w:p>\n    
        <w:p w:rsidR="003F4CA3" w:rsidRDefault="00E55BE5" w:rsidP="00084F90">\n      
            <w:pPr>\n        
                <w:ind w:left="720" w:firstLine="720"/>\n      
            </w:pPr>\n      
            <w:r>\n        
                <w:rPr>\n          
                    <w:sz w:val="36"/>\n          
                    <w:szCs w:val="36"/>\n          
                    <w:u w:val="single"/>\n        
                </w:rPr>\n        
                <w:t xml:space="preserve">It is used </w:t>\n      
            </w:r>\n      
            <w:r w:rsidR="00084F90" w:rsidRPr="00B6714F">\n        
                <w:rPr>\n          
                    <w:sz w:val="36"/>\n          
                    <w:szCs w:val="36"/>\n          
                    <w:u w:val="single"/>\n        
                </w:rPr>\n        
                <w:t>for te</w:t>\n      
            </w:r>\n      
            <w:r w:rsidR="00084F90">\n        
                <w:rPr>\n          
                    <w:sz w:val="36"/>\n          
                    <w:szCs w:val="36"/>\n          
                    <w:u w:val="single"/>\n        
                </w:rPr>\n        
                <w:t>s</w:t>\n      
            </w:r>\n      
            <w:r w:rsidR="00084F90" w:rsidRPr="00B6714F">\n        
                <w:rPr>\n          
                    <w:sz w:val="36"/>\n          
                    <w:szCs w:val="36"/>\n          
                    <w:u w:val="single"/>\n        
                </w:rPr>\n        
                <w:t>ting.</w:t>\n      
            </w:r>\n      
            <w:bookmarkStart w:id="0" w:name="_GoBack"/>\n      
            <w:bookmarkEnd w:id="0"/>\n    
        </w:p>\n    
        <w:sectPr w:rsidR="003F4CA3">\n      
            <w:pgSz w:w="11906" w:h="16838"/>\n      
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>\n      
            <w:cols w:space="708"/>\n      
            <w:docGrid w:linePitch="360"/>\n    
        </w:sectPr>\n  
    </w:body>\n
</w:document>\n    

我想用值替换字典的键,比如用检查进行测试,它被分成三个不同的运行,可以在 xml 文件中看到。有没有什么方法可以结合单词测试的所有拆分或任何通用方法来克服这个问题。

这是python代码

from lxml import etree
import xml.etree.ElementTree as ET
import zipfile
d = {"dock":"docx","testing":"checking"}

docx_filename = "Test.docx"

def get_word_xml(docx_filename):
   zip = zipfile.ZipFile(docx_filename)
   xml_content = zip.read('word/document.xml')
   return xml_content

def get_xml_tree(xml_string):
   return etree.fromstring(xml_string)


xml_string = get_word_xml(docx_filename)
xmltree = get_xml_tree(xml_string)
my_etree = etree.tostring(xmltree, pretty_print=True)

def _check_element_is(element, type_char):
     word_schema = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
     return element.tag == '{%s}%s' % (word_schema,type_char)
 
e = ET.ElementTree(xmltree)
for elt in e.iter():
    if _check_element_is(elt, 't'):
        print(elt.text)

我得到的输出是

这是一个码头

文档

.

它被使用

为你

s

亭。

我想要的是

这是一个停靠文件。

它用于测试。

标签: pythonopenxmldocxpython-docxdocx4j

解决方案


推荐阅读