程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Win32 Python find text specify format win32com locate word start end equivalent to send shortcut key

編輯:Python
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’)

Equate to

s.Find.Execute(FindText=‘15’)

This has only one parameter , There is no problem of omitting parameters

2. Use regular lookup 1915 perhaps 2015 Such a string ( Find from current position to end )

s.Find.Execute(“??15”, False, False, True)

lookup 2015 perhaps 1915 Such a string ,? Represents any character

Equate to s.Find.Execute(

FindText=“??15”, MatchCase=False,

MatchWholeWord=False, MatchWildcards=True

)

Other parameters cannot be omitted

s.Find.Execute(FindText=“??15”, MatchWildcards=True)

That is, in order , You used the 4 Parameters , So the first 1, 2, 3 None of the parameters can be omitted

3. Compared with the previous article, the circular search function is added

s.Find.Execute(“??15”, False, False, True, False, False, True, 1)

lookup 2015 perhaps 1915 Such a string ,? Represents any character

Equate to s.Find.Execute(

FindText=“??15”, MatchCase=False,

MatchWholeWord=False, MatchWildcards=True,

MatchSoundsLike=False, MatchAllWordForms=False,

Forward=True, Wrap=1

)

Wrap Out of commission True, Only use 1, This is because this parameter is in COM Enum type in , For details, please refer to the document

Other parameters cannot be omitted

s.Find.Execute(FindText=“??15”, MatchWildcards=True, Forward=True, Wrap=1)
4. hold 15 Switch to 16

The replacement must be carried out with the 11 Parameters , that 1 To 10 All parameters of the must be written with .

s.Find.Execute(“15”, False, False, False, False, False, True, 1, False, “16”, 2)

Other parameters cannot be omitted

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

  • Represents any number of characters
    [x-x] Any single character in the specified range , Such as [a-c]pple representative apple, bpple, cpple.
    ^# Any single number , be equal to [0-9];
    ^$ Any single letter , be equal to [a-zA-Z];
    {n} The number of previous characters is n
    {m, n} The minimum number of previous characters is m, At most n

Reference resources
https://zhuanlan.zhihu.com/p/67543981

Very important
https://www.xin3721.com/Python/python21722.html


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved