python操作word首行缩进

  • 发布时间: 2024-03-02 10:21:54
  • 作者: zhan
  • 分类标签: Python
  • 阅读数量: 74
  • 评论数量: 0
def set_indent(paragraph, left_indent=None, right_indent=None, first_line_indent=None, hanging_indent=None):
    """
    设置缩进,单位为字符
    :param paragraph: 段落
    :param left_indent: 左缩进
    :param right_indent: 右缩进
    :param first_line_indent: 首行缩进, 大于0
    :param hanging_indent: 悬挂缩进,小于0
    """
    assert not all([first_line_indent, hanging_indent]), '首行缩进与悬挂缩进不可同时设置'
    pPr = paragraph._element.get_or_add_pPr()
    ind = OxmlElement('w:ind')
    if left_indent:
        ind.set(qn('w:leftChars'), str(left_indent * 100))
    if right_indent:
        ind.set(qn('w:rightChars'), str(right_indent * 100))
    if first_line_indent:
        ind.set(qn('w:firstLineChars'), str(first_line_indent * 100))
    if hanging_indent:
        ind.set(qn('w:hangingChars'), str(hanging_indent * 100))
    pPr.append(ind)

提交评论

您尚未登录,登录之后方可评论~ 登录 or 注册

评论列表

暂无评论