我是python-docx的新手,我想在一行中同时对齐左缩进和右缩进。但我找不到一个例子来说明如何做到这一点。有人能帮帮我吗?
我想为公司和职位添加一行,如“谷歌工程师”,我希望“谷歌”在一行中左缩进,“工程师”右缩进。如何通过在paragraph_format中添加tabstop在python-docx中完成此操作?
发布于 2020-04-02 05:37:08
是的,你可以通过添加制表位来解决这个问题。
如果您查看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")希望这能帮上忙,贝斯特
https://stackoverflow.com/questions/58656450
复制相似问题