from win32com.client import Dispatch
word_path = r'D:\KKCap\1.docx'
app = Dispatch("Word.Application")
doc = app.Documents.Open(word_path)
app.Visible = 1
app.DisplayAlerts = 0
s = app.Selection
# lookup Aaaa The first place to appear
s.Find.Execute('Aaaa')
# Set to first level title
s.Style = -2
print(s.Style)
Styles The return parameter of , title 1、 title 2、 title 3 Respectively -2、-3、-4, The header is -32, The title is -63, See you for the rest Styles file
Use s.Find.Execeute When the method is used , If you use a parameter at any position , You have to write all the previous parameters , Even specifying parameters is not enough . The following points 4 A description of the situation
1. lookup 15
s.Find.Execute(‘15’)
s.Find.Execute(FindText=‘15’)
2. Use regular lookup 1915 perhaps 2015 Such a string ( Find from current position to end )
s.Find.Execute(“??15”, False, False, True)
s.Find.Execute(FindText=“??15”, MatchWildcards=True)
3. Compared with the previous article, the circular search function is added
s.Find.Execute(“??15”, False, False, True, False, False, True, 1)
s.Find.Execute(FindText=“??15”, MatchWildcards=True, Forward=True, Wrap=1)
4. hold 15 Switch to 16
s.Find.Execute(“15”, False, False, False, False, False, True, 1, False, “16”, 2)
s.Find.Execute(FindText=“15”, ReplaceWith=“16”, Replace=2)
Only the reason why the parameter cannot be omitted , Maybe it's because we need to python Parameter value of , Cross language conversion to C++ Of COM type , Communication complexity leads to .
Specific regular syntax , See the reference article at the end of the article , Only part of the syntax is listed below
Reference resources
https://zhuanlan.zhihu.com/p/67543981
Very important
https://www.xin3721.com/Python/python21722.html