Use automation.py automation to rename PDF bookmarks so that all uppercase letters are capitalized in the first letter

Source: Internet
Author: User

Today downloaded an English PDF book, but the bookmark is all uppercase English letters, looks a bit awkward, so try to use Automation to rename PDF bookmarks,

Make the bookmark all the first letter capitalized.

PDF original bookmarks such as:

Renamed PDF bookmarks

Automation dynamics, two or three minutes to complete the renaming work.

The following is a description of the automation process.

automation.py is the module of my encapsulated Python call uiautomation, reference http://www.cnblogs.com/Yinkaisheng/p/3444132.html

Run Automation.py-h View Help

First use Automation.py-a to get the hierarchy of the PDF bookmark tree control, the-a parameter is to get the control under the cursor and always get the parent control until the top-level window

After entering automation.py-a enter in cmd, move the mouse to the PDF bookmark immediately, and after 3 seconds, the control tree will be printed out

You can then use this method to get the right-click PDF Bookmark Right-click menu of the control tree hierarchy.

The final code is as follows:

#!python3#-*-coding:utf-8-*-# rename pdf bookmarks with Foxitreader 6.2.3import timeimport stringimport automationtree Depth = 2 #书签树只有前两层需要重命名UpperWords = {' AMD ': ' AMD ', ' ARP ': ' arp ', ' DHCP ': ' DHCP ', ' DNS ': ' DNS ', ' IP ': ' IP ' , ' mac ': ' Mac ', ' UNIX ': ' Unix ', ' pc ': ' PC ', ' PCs ': ' PCs ', ' TCP ': ' TCP ', ' The TCP/IP ': ' "TCP/IP ', ' vs ': ' vs ' ',}lowerwords = [' A ', ' an ', ' and ', ' at ', ' for ', ' in ', ' of ', ' the ', ' to ']class BookMark (): def-__init__ (self, name, NewName): self.name = name Self.newname = NewName Self.children = []def main (): Window = Automatio N.windowcontrol (searchdepth= 1, classname= ' classfoxitreader ') window. SetActive () time.sleep (1) tree = Automation. Treecontrol (searchfromcontrol= window, classname= ' SysTreeView32 ') ChildItems = tree. GetChildren () BookMarks = [] depth = 1 for treeItem in childitems:if treeitem.controltype = = Automation.c OntrolType.TreeItemControl:RenameTreeItem (treE, TreeItem, BookMarks, depth) Fout = open (' Rename_pdf_bookmark.txt ', ' wt ', encoding= ' utf-8 ') depth = 1 for book Mark in Bookmarks:dumpbookmark (fout, bookmark, depth) fout.close () def dumpbookmark (Fout, bookmark, depth): F Out.write ("* (depth-1) * 4 + bookmark.newname + ' \ n ')" for child in BookMark.children:DumpBookMark (Fout, CH ILD, Depth + 1) def renametreeitem (tree, TreeItem, BookMarks, depth): Treeitem.scrollintoview () if depth > TREEDEP Th:return name = Treeitem.name NewName = Rename (name) bookmark = Bookmark (name, newName) Bookmarks.app End (bookmark) if newName! = Name:treeItem.RightClick () # Foxitreader Bookmark Right-click menu (Bcgptoolbar, non-Windows menu) after popup, enumerate Not to the menu, but from the screen point Controlfrompoint can get to the menu, Todo # takes special processing to get the Rename menu Time.sleep (0.2) x, y = automation. Win32api.getcursorpos () MenuItem = automation. Controlfrompoint (x + 2, y + 2) if Menuitem.controltype = = Automation. Controltype.menuitemcontrol:            #鼠标右下方弹出菜单 while not (menuitem.name = = ' Rename (R) ' or menuitem.name = = ' Rename '): Y + = MenuItem = Automation. Controlfrompoint (x + 2, y) Else: #鼠标右上方弹出菜单 MenuItem = automation.                Controlfrompoint (x + 2, y-2) while not (menuitem.name = = ' Rename (R) ' or menuitem.name = = ' Rename '): Y-= MenuItem = Automation. Controlfrompoint (x + 2, y) menuitem.click () edit = Automation. Editcontrol (searchfromcontrol= tree, searchdepth= 1) edit. SetValue (newName) automation.        Win32api.sendkeys (' {Enter} ') print (' rename "{0}" to "{1}" '. Format (name, newName)) if depth + 1 > treedepth:        Return Treeitem.expand () ChildItems = Treeitem.getchildren () if ChildItems:treeItem.Expand ()  For the Childitems:renametreeitem (tree, Child, Bookmark.children, depth + 1) def Rename (name): NewName = Name.strip (). replace (' \ n ',") #将CHAPTER 10 becomes 10, remove the front CHAPTER if Newname.startswith (' CHAPTER '): NewName = Newname[len (' CHAPTER '):] n Ewname = Newname.title () words = Newname.split () Skipindex = 1 if words[0][-1].isdigit () else 0 for I in range (le n (words)): Lowerword = Words[i].lower () start_punctuation = ' End_punctuation = ' if Lowerwo Rd[0] in string.punctuation:start_punctuation = lowerword[0] Lowerword = lowerword[1:] If lo        Werword[-1] in string.punctuation:end_punctuation = lowerword[-1] Lowerword = lowerword[:-1]            If Lowerword in upperwords:words[i] = start_punctuation + Upperwords[lowerword] + end_punctuation Continue if I > Skipindex and Lowerword in Lowerwords:if words[i-1][-1]! = ': ': Words  [I] = lowerword newName = '. Join (words) return newnameif __name__ = ' __main__ ': Main () input (' \npress enter To exit ')

Code can be downloaded from github

In addition, this code is implemented according to FoxReader6.2.3, and other versions are not necessarily supported.

There are also running this program to close the Thunderbolt, in the test found that if the run Thunderbolt, will make the PDF right-click menu to get a problem.

Use automation.py automation to rename PDF bookmarks so that all uppercase letters are capitalized in the first letter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.