python+word一键修改“式中”未顶格,图&题居中但有缩进,公式有缩进等问题

 图&题居中但有缩进

from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH

def apply_centered_style_to_equations(doc_path, style_name):
    doc = Document(doc_path)

    for paragraph in doc.paragraphs:
        if paragraph.alignment == WD_ALIGN_PARAGRAPH.CENTER:
            # print("找到居中段")
            print("居中段取消缩进")
            paragraph.style = "居中"
            paragraph.paragraph_format.first_line_indent = 0  # 取消首行缩进
            paragraph.paragraph_format.left_indent = 0  # 取消悬挂缩进




    # 保存修改后的文档
    new_doc_path = r'D:\实验室\省自然基金\系统提交\modified_1.docx'
    doc.save(new_doc_path)
    return new_doc_path


def main():
    doc_path = r'D:\实验室\省自然基金\系统提交\1.docx'  # 替换为你的 Word 文档路径
    style_name = '居中'  # 替换为你的自定义样式名称

    new_doc_path = apply_centered_style_to_equations(doc_path, style_name)
    print(f"修改后的文档保存为: {new_doc_path}")


if __name__ == "__main__":
    main()

作用:

将居中的段落(图片、图题、表题)首行缩进取消。需要pip install python-docx

思路:

检测居中的段落,然后首行缩进、悬挂缩进设置为0

p.s:

也可以任意选中一个,例如想将所有居中的图题首行缩进去掉,选中文档中的一个

再选“选定所有格式类似的文本”


效果如下:

然后就能一键设置样式,或者单独调段落、字体格式了


“式中”未顶格 

from docx import Document


def apply_centered_style_to_equations(doc_path, style_name):
    doc = Document(doc_path)

    for paragraph in doc.paragraphs:
        # 检查段落是否包含一个制表符,并假定它是一个公式行
        print(paragraph.text)


        if paragraph.text.startswith('式中'):

            # 将自定义样式应用到该段落
            print("找到式中段!")
            paragraph.style = style_name
            paragraph.paragraph_format.first_line_indent = 0  # 取消首行缩进
            paragraph.paragraph_format.left_indent = 0  # 取消悬挂缩进

    # 保存修改后的文档
    new_doc_path = r'D:\实验室\省自然基金\系统提交\modified_test.docx'
    doc.save(new_doc_path)
    return new_doc_path


def main():
    doc_path = r'D:\实验室\省自然基金\系统提交\test.docx'  # 替换为你的 Word 文档路径
    style_name = '式中'  # 替换为你的自定义样式名称

    new_doc_path = apply_centered_style_to_equations(doc_path, style_name)
    print(f"修改后的文档保存为: {new_doc_path}")


if __name__ == "__main__":
    main()

作用:

将公式后的“式中”段落顶格写

思路:

检测以“式中”二字起头的段落,然后首行缩进、悬挂缩进设置为0


其他常用一键智能遍历操作方法

1. 检测文档中的英文缩写,防止漏了全称解释

上面就能检测3个字母的英文缩写,如果是四个如下:


2. 找到word中所有公式调整样式

当然,也可以用python检测段落是否有2个制表符以定位到公式行,然后,统一改样式(前提是word已有这个样式),或单独调居中,缩进啥的

from docx import Document


def apply_centered_style_to_equations(doc_path, style_name):
    doc = Document(doc_path)

    for paragraph in doc.paragraphs:
        # 检查段落是否包含一个制表符,并假定它是一个公式行
        print(paragraph.text)
        if '\t' in paragraph.text:

            # 进一步检查段落是否只有一个制表符,并且制表符前有内容
            if paragraph.text.count('\t') == 2:
                # 将自定义样式应用到该段落
                print("找到公式!")
                paragraph.style = style_name

    # 保存修改后的文档
    new_doc_path = r'D:\实验室\省自然基金\系统提交\modified_1.docx'
    doc.save(new_doc_path)
    return new_doc_path


def main():
    doc_path = r'D:\实验室\省自然基金\系统提交\1.docx'  # 替换为你的 Word 文档路径
    style_name = '公式居中'  # 替换为你的自定义样式名称

    new_doc_path = apply_centered_style_to_equations(doc_path, style_name)
    print(f"修改后的文档保存为: {new_doc_path}")


if __name__ == "__main__":
    main()

3. 一键更改标题序号样式

科研材料各级标题序号一般如下,如果不小心顺序反了,可以使用通配符一键修改

一、(一)、1、(1)、1)

如下图,是把选中段落中1、2、3、换成(1)(2)(3)的方法


4.叠词查找

AA型中文叠词

([一-龥])\1

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值