使用python docx模块时减少段落功能的垂直空间

2024-04-19 16:26:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我添加了单行文本,类似于string=“this is just some text.”使用文件段落函数到word文档。它工作得很好,除了我想减少文本之间的垂直间距。有什么办法吗。在

谢谢。在


Tags: 文件函数text文档文本stringissome
1条回答
网友
1楼 · 发布于 2024-04-19 16:26:15

我已经创建了一个测试docx,前两段以正常间距隔开,下两段以更大间距隔开:

enter image description here

前两段结构如下(文档.xml,请参阅Tag Wiki

<w:p w:rsidR="009565D8" w:rsidRDefault="009336B0">
<w:r>
<w:t>Test</w:t>
</w:r>
</w:p>
<w:p w:rsidR="009336B0" w:rsidRDefault="009336B0">
<w:r>
<w:t>
Test
</w:t>
</w:r>
</w:p>

带间距的段落如下所示:

^{pr2}$

如您所见,有一个<w:pPr>标记,它包含标记的属性,包括间距。在

现在您应该能够更改docx.py文件通过更改段落函数。在

以下是段落功能:

def paragraph(paratext, style='BodyText', breakbefore=False, jc='left'):
    """
    Return a new paragraph element containing *paratext*. The paragraph's
    default style is 'Body Text', but a new style may be set using the
    *style* parameter.

    @param string jc: Paragraph alignment, possible values:
                      left, center, right, both (justified), ...
                      see http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html
                      for a full list

    If *paratext* is a list, add a run for each (text, char_format_str)
    2-tuple in the list. char_format_str is a string containing one or more
    of the characters 'b', 'i', or 'u', meaning bold, italic, and underline
    respectively. For example:

        paratext = [
            ('some bold text', 'b'),
            ('some normal text', ''),
            ('some italic underlined text', 'iu')
        ]
    """
    # Make our elements
    paragraph = makeelement('p')

    if not isinstance(paratext, list):
        paratext = [(paratext, '')]
    text_tuples = []
    for pt in paratext:
        text, char_styles_str = (pt if isinstance(pt, (list, tuple))
                                 else (pt, ''))
        text_elm = makeelement('t', tagtext=text)
        if len(text.strip()) < len(text):
            text_elm.set('{http://www.w3.org/XML/1998/namespace}space',
                         'preserve')
        text_tuples.append([text_elm, char_styles_str])
    pPr = makeelement('pPr')
    pStyle = makeelement('pStyle', attributes={'val': style})
    pJc = makeelement('jc', attributes={'val': jc})
    pPr.append(pStyle)
    pPr.append(pJc)

    ###   Added Content from edi9999:
        Here you should add a children to the pPr element
        spacing= makeelement('spacing',attributes={'line':480,'lineRule':'auto'})
        pPr.append(spacing)
    ###

    # Add the text to the run, and the run to the paragraph
    paragraph.append(pPr)
    for text_elm, char_styles_str in text_tuples:
        run = makeelement('r')
        rPr = makeelement('rPr')
        # Apply styles
        if 'b' in char_styles_str:
            b = makeelement('b')
            rPr.append(b)
        if 'i' in char_styles_str:
            i = makeelement('i')
            rPr.append(i)
        if 'u' in char_styles_str:
            u = makeelement('u', attributes={'val': 'single'})
            rPr.append(u)
        run.append(rPr)
        # Insert lastRenderedPageBreak for assistive technologies like
        # document narrators to know when a page break occurred.
        if breakbefore:
            lastRenderedPageBreak = makeelement('lastRenderedPageBreak')
            run.append(lastRenderedPageBreak)
        run.append(text_elm)
        paragraph.append(run)
    # Return the combined paragraph
    return paragraph

相关问题 更多 >