import win32com.client
import re
def compile_change_Word_titlestyle():
# 连接到 Word 应用程序
word_app = win32com.client.Dispatch("Word.Application")
# 获取活动文档
active_document = word_app.ActiveDocument
# 定义匹配规则
pattern_title_2 = re.compile(r'\d+\.\d+\. ')
pattern_title_3 = re.compile(r'\d+\.\d+\.\d+')
# 遍历文档中的每个段落
for index in range(1, active_document.Paragraphs.Count + 1):
paragraph = active_document.Paragraphs(index)
text = paragraph.Range.Text
# 检查段落是否符合“数字.数字. ”格式
if pattern_title_2.match(text):
paragraph.Style = active_document.Styles("标题 2")
# 检查段落是否符合“数字.数字.数字”的格式
elif pattern_title_3.match(text):
paragraph.Style = active_document.Styles("标题 3")
# 保存文档
active_document.Save()
if __name__ == '__main__':
compile_change_Word_titlestyle()
pyinstaller --onefile compile_change_Word_titlestyle.py
参考文章:
这个很全:https://blog.csdn.net/qq_45464895/article/details/123173742
http://t.csdn.cn/CjZqE
https://blog.csdn.net/hihell/article/details/121966945
https://www.cnblogs.com/1gaoyu/p/12656003.html
https://www.5axxw.com/wiki/content/kbhv5m
https://www.5axxw.com/wiki/topic/l8oj0b
https://www.5axxw.com/wiki/content/qa5cih
https://www.cnblogs.com/wangyueping/p/16018206.html
https://blog.csdn.net/weixin_47383889/article/details/119847787
https://blog.csdn.net/u012034742/article/details/106022019/
https://blog.csdn.net/qq_27017791/article/details/108897521
http://tool.chinaz.com/Tools/PageColor.aspx
https://www.zhihu.com/question/511536143/answer/2313422603
————————————————
版权声明:本文为CSDN博主「zzzxydq」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ben_na_/article/details/124240275