在python docx模块中使用段落功能时,减少垂直空间 [英] Decreasing the vertical space when using the paragraph function in the python docx module

查看:64
本文介绍了在python docx模块中使用段落功能时,减少垂直空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加单行文本,例如此字符串=这只是一些文本."将docx.paragraph函数用于Word文档.它工作正常,除了我想使文本之间的垂直间距更小.任何想法我该怎么做.

I am adding single line text something like this string = "This is just some text." using the docx.paragraph function to a word document. It works fine, except I would like to make the vertical spacing between the texts less. Any ideas how I can do that.

谢谢.

推荐答案

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

I have created a test docx, the first two paragraphs are spaced with normal spacing, and the two next paragraph are spaced with more spacing:

前两段具有以下结构(document.xml,请参见 Tag Wiki )

The first two paragraphs have the following structure (document.xml, see the 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>

带间距的段落如下:

<w:p w:rsidR="009336B0" w:rsidRDefault="009336B0" w:rsidP="009336B0">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:t>Test2</w:t>
</w:r>
</w:p>
<w:p w:rsidR="009336B0" w:rsidRDefault="009336B0" w:rsidP="009336B0">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:t>Test2</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>

如您所见,有一个< w:pPr> 标签,其中包含标签的属性,包括间距.

As you can see, there is a <w:pPr> Tag that contains the properties of the tag, including the spacing.

您现在应该可以通过更改 paragraph 函数来更改docx.py.

You should now be able to change the docx.py by changing the paragraph function.

这是段落功能:

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

这篇关于在python docx模块中使用段落功能时,减少垂直空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆