-
Notifications
You must be signed in to change notification settings - Fork 463
/
rename_pdf_bookmark.py
284 lines (266 loc) · 9.94 KB
/
rename_pdf_bookmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!python3
# -*- coding: utf-8 -*-
#rename pdf bookmarks with FoxitReader 7.2.4, 参考: http://www.cnblogs.com/Yinkaisheng/p/4820954.html
import os
import sys
import time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # not required after 'pip install uiautomation'
import uiautomation as automation
TreeDepth = 1 #书签树需要命名的层数
TreeSkipDepthList = []
RenameFunction = None
LowerWords = ['a', 'an', 'and', 'at', 'for', 'in', 'of', 'on', 'the', 'to', 'with', 'from']
MatchWords = ['chapter', 'ch', 'section', 'hour', 'lession', 'tutorial']#, 'step'
Punctuation = '!"\',:;?)' # '!"\',.:;?)'
PunctuationPostfix = '!,:;?)'
Renamed = False
UpperWords = {
'amd': 'AMD',
'api': 'API',
'apis': 'APIs',
'arp': 'ARP',
'dhcp': 'DHCP',
'dns': 'DNS',
'e-mail': 'E-mail',
'e-mails': 'E-mails',
'ee': 'EE',
'eula' : 'EULA',
'http': 'HTTP',
'ip': 'IP',
'mac': 'MAC',
'unix': 'UNIX',
'pc': 'PC',
'pcs': 'PCs',
'tcp': 'TCP',
'tcp/ip': 'TCP/IP',
'udp': 'UDP',
'uml': 'UML',
'i': 'I',
'ii': 'II',
'iii': 'III',
'iv': 'IV',
'v': 'V',
'vi': 'VI',
'vii': 'VII',
'viii': 'VIII',
'ix': 'IX',
'x': 'X',
'xi': 'XI',
'xii': 'XII',
'xiii': 'XIII',
'xiv': 'XIV',
'xv': 'XV',
}
class BookMark():
def __init__(self, name, newName):
self.name = name
self.newName = newName
self.children = []
def BatchRename():
foxitWindow = automation.WindowControl(searchDepth= 1, ClassName= 'classFoxitReader')
foxitWindow.ShowWindow(automation.ShowWindow.ShowMaximized)
ret = foxitWindow.SetActive()
print('SetActive', ret)
automation.Logger.Log(foxitWindow.Name[:-len(' - Foxit Reader')] + '\n')
automation.SendKeys('{Ctrl}0')
for aid in ['59583', '60682', '60683']:
editToolBar = automation.ToolBarControl(searchFromControl= foxitWindow, AutomationId = aid, Name = 'Caption Bar')
if editToolBar.Exists(0, 0):
editToolBar.Click(-40, 0.5)
automation.SendKeys('{Alt}y')
paneWindow = automation.WindowControl(searchFromControl= foxitWindow, AutomationId = '65280')
bookmarkPane = automation.PaneControl(searchFromControl= paneWindow, searchDepth= 1, foundIndex= 1)
l, t, r, b = bookmarkPane.BoundingRectangle
#bookmarkButton = automation.ButtonControl(searchFromControl= bookmarkPane, Name = '书签') # can't find, but automation -a can find it, why
if bookmarkPane.Name == '书签':
if r - l < 40:
bookmarkButton = automation.ControlFromPoint(l + 10, t + 40)
if bookmarkButton.Name == '书签':
bookmarkButton.Click(simulateMove= False)
else:
bookmarkButton = automation.ControlFromPoint(l + 10, t + 40)
if bookmarkButton.Name == '书签':
bookmarkButton.Click(simulateMove= False)
tree = automation.TreeControl(searchFromControl= foxitWindow, ClassName= 'SysTreeView32')
childItems = tree.GetChildren()
bookMarks = []
depth = 1
for treeItem in childItems:
if treeItem.ControlType == automation.ControlType.TreeItemControl:
RenameTreeItem(tree, treeItem, bookMarks, depth)
fout = open('rename_pdf_bookmark.txt', 'wt', encoding= 'utf-8')
depth = 1
for bookMark in bookMarks:
DumpBookMark(fout, bookMark, depth)
fout.close()
if Renamed:
automation.Logger.Log('rename pdf: ' + foxitWindow.Name)
automation.SendKeys('{Ctrl}s')
while '*' in foxitWindow.Name:
time.sleep(0.5)
def DumpBookMark(fout, bookMark, depth):
fout.write(' ' * (depth - 1) * 4 + bookMark.newName + '\n')
for child in bookMark.children:
DumpBookMark(fout, child, depth + 1)
def RenameTreeItem(tree, treeItem, bookMarks, depth, removeChapter = True):
treeItem.ScrollIntoView()
if depth > TreeDepth:
return
name = treeItem.Name
if not name.strip():
return
newName, alert = RenameFunction(name, removeChapter)
if newName.startswith('Appendix'):
removeChapter = False
bookMark = BookMark(name, newName)
bookMarks.append(bookMark)
if newName != name and depth not in TreeSkipDepthList:
global Renamed
Renamed = True
time.sleep(0.1)
find = False
for i in range(3):
treeItem.RightClick(simulateMove = False, waitTime = 0.2)
# FoxitReader书签右键菜单(BCGPToolBar,非Windows菜单)弹出后,枚举不到菜单,但从屏幕点上ControlFromPoint能获取到菜单, todo
# 采用特殊处理获取重命名菜单
x, y = automation.Win32API.GetCursorPos()
menuItem = automation.ControlFromPoint(x + 2, y + 2)
if isinstance(menuItem, automation.MenuItemControl):
#鼠标右下方弹出菜单
while 1:
if menuItem.Name == '重命名(R)' or menuItem.Name == 'Rename':
find = True
break
y += 20
menuItem = automation.ControlFromPoint(x + 2, y)
if not isinstance(menuItem, automation.MenuItemControl):
break
else:
#鼠标右上方弹出菜单
menuItem = automation.ControlFromPoint(x + 2, y - 2)
while 1:
if menuItem.Name == '重命名(R)' or menuItem.Name == 'Rename':
find = True
break
y -= 20
menuItem = automation.ControlFromPoint(x + 2, y)
if not isinstance(menuItem, automation.MenuItemControl):
break
if find:
break
else:
automation.SendKeys('{Esc}')
time.sleep(0.5)
if not find:
automation.Logger.Log('this pdf not support editing')
exit(0)
menuItem.Click(simulateMove = False, waitTime = 0.2)
edit = automation.EditControl(searchFromControl= tree, searchDepth= 1)
edit.SetValue(newName, waitTime = 0.05)
automation.SendKeys('{Enter}', waitTime = 0.05)
automation.Logger.Write('rename: ')
automation.Logger.WriteLine(name, automation.ConsoleColor.Green)
automation.Logger.Write(' @@to : ' if alert else ' to: ')
automation.Logger.WriteLine(newName, automation.ConsoleColor.Yellow if alert else automation.ConsoleColor.Green)
if depth + 1 > TreeDepth:
return
if automation.ExpandCollapseState.Collapsed == treeItem.CurrentExpandCollapseState():
treeItem.Expand(0.1)
childItems = treeItem.GetChildren()
if childItems:
for child in childItems:
RenameTreeItem(tree, child, bookMark.children, depth + 1, removeChapter)
def Rename1(name, removeChapter = True):
alert = False
newName = name.strip().replace('‘', '\'').replace('’', '\'').replace('“', '"').replace('”', '"')
#newName = newName.replace('.', ' ')
#newName = newName.replace('_ _', '__')
#newName = newName.replace('_ _', '__')
words = newName.split()
if len(words) == 0:
return '', alert
firstWord = words[0].lower()
if removeChapter and firstWord in MatchWords and len(words) > 2:
del words[0]
i = 0
while i < len(words):
if words[i][0] in Punctuation:
if i > 0 and len(words[i]) == 1:
alert = True
words[i-1] += words[i]
del words[i]
i -= 1
continue
i += 1
newName = ' '.join(words)
return newName, alert
def Rename2(name, removeChapter = True):
alert = False
newName = name.strip().replace('‘', '\'').replace('’', '\'').replace('“', '"').replace('”', '"')
if newName.startswith('www.'):
return newName, alert
newName = newName.title()
words = newName.split()
if len(words) == 0:
return '', alert
firstWord = words[0].lower()
if removeChapter and firstWord in MatchWords and len(words) > 2:
del words[0]
skipIndex = 1 if words[0][-1].isdigit() else 0
i = 0
while i < len(words):
lowerWord = words[i].lower()
if lowerWord[0] in PunctuationPostfix:
if i > 0 and len(words[i]) == 1:
alert = True
words[i-1] += words[i]
del words[i]
i -= 1
continue
start_punctuation = ''
end_punctuation = ''
if lowerWord[0] in Punctuation:
start_punctuation = lowerWord[0]
lowerWord = lowerWord[1:]
if lowerWord[-1] in Punctuation:
end_punctuation = lowerWord[-1]
lowerWord = lowerWord[:-1]
if lowerWord in UpperWords:
words[i] = start_punctuation + UpperWords[lowerWord] + end_punctuation
i += 1
continue
if i > skipIndex and lowerWord in LowerWords:
if words[i-1][-1] != ':':
words[i] = lowerWord
i += 1
newName = ' '.join(words)
return newName, alert
def usage():
sys.stdout.write("""usage
-h show command help
-r rename function, 1 or 2
-d bookmark tree depth
""")
def main():
BatchRename()
if __name__ == '__main__':
import getopt
sys.stdout.write(str(sys.argv) + '\n')
options, args = getopt.getopt(sys.argv[1:], 'hr:d:s:',
['help', 'rename=', 'depth=', 'skip='])
RenameFunction = Rename1
for (o, v) in options:
if o in ('-h', '-help'):
usage()
exit(0)
elif o in ('-r', '-rename'):
rename = int(v)
if rename == 2:
RenameFunction = Rename2
elif o in ('-d', '-depth'):
TreeDepth = int(v)
elif o in ('-s', '-skip'):
skipDepth = v.split(', ')
TreeSkipDepthList = [int(it) for it in skipDepth]
main()
#input('\npress enter to exit')