【问题标题】:How to use tabletop by python-docx如何通过 python-docx 使用桌面
【发布时间】:2020-04-02 05:37:08
【问题描述】:

我是 python-docx 的新手,我想在一行中对齐左缩进和右缩进。但我找不到一个例子来展示如何做到这一点。有人可以帮我吗?

我想为公司和职位添加一行,例如“Google Engineer”,我希望“Google”与左缩进对齐,“Engineer”与右缩进对齐在一行中。如何在 python-docx 中通过在段落格式中添加制表位来做到这一点?

【问题讨论】:

    标签: python-docx tabstop


    【解决方案1】:

    是的,您可以通过添加制表位来解决这个问题。

    如果您查看the picture,首先您需要计算添加制表位的位置。如果您想让Engineer在同一行(段落)内向右对齐,则需要根据页面宽度和左右边距计算端点。

    然后,一旦你有了它,在添加制表位时设置WD_TAB_ALIGNMENT.RIGHT 很重要,这将确保内容右对齐并“粘”在右侧。

    这是您的案例的示例代码:

    import docx
    doc = docx.Document()
    
    p = doc.add_paragraph('Google\tEngineer')  # tab will trigger tabstop
    sec = doc.sections[0]
    # finding end_point for the content 
    margin_end = docx.shared.Inches(
        sec.page_width.inches - (sec.left_margin.inches + sec.right_margin.inches))
    tab_stops = p.paragraph_format.tab_stops
    # adding new tab stop, to the end point, and making sure that it's `RIGHT` aligned.
    tab_stops.add_tab_stop(margin_end, docx.enum.text.WD_TAB_ALIGNMENT.RIGHT)
    
    doc.save("test.docx")
    
    

    希望这会有所帮助, 最好的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多