Guest User

Untitled

a guest
Nov 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #!python
  2. # coding: utf-8
  3.  
  4. import os
  5. import win32com.client
  6.  
  7. class PostProcedure(object):
  8. def __init__(self):
  9. pass
  10.  
  11. def attach(self):
  12. self.wordApp = win32com.client.Dispatch("Word.Application")
  13. self.wordApp.Visible = True
  14. self.doc = self.wordApp.ActiveDocument
  15. print(os.path.basename(self.doc.FullName))
  16.  
  17. def extractBookmarks(self):
  18. bookmarks = {}
  19. for bookmark in self.doc.Bookmarks:
  20. # print("{}".format(bookmark.Name))
  21. bookmarks[bookmark.Name] = 0
  22. return bookmarks
  23.  
  24. def taskAddBookmarkForRegisters(self):
  25. bookmarks = self.extractBookmarks()
  26. for para in self.doc.Paragraphs:
  27. for word in para.Range.Words:
  28. text = word.Text.strip()
  29. if text in bookmarks:
  30. #print("{}:{} (Bookmarks: {}, Hyperlinks: {})".format(word.Start, word.End, word.Bookmarks.Count, word.Hyperlinks.Count))
  31. if word.Bookmarks.Count == 0 and word.Hyperlinks.Count == 0:
  32. bookmarks[text] += 1
  33. print("\t\tAdd bookmark: {}; count={}".format(word.Text, bookmarks[text]))
  34. self.doc.Hyperlinks.Add(Anchor=word, Address="#{}".format(text))
  35.  
  36.  
  37. def run(self):
  38. """Execute all methods leading with keyword 'task' in this class."""
  39. self.attach()
  40. # print('\n'.join(dir(self.doc)))
  41. for name in self.__dir__():
  42. attr = getattr(self, name)
  43. if name.startswith('task') and callable(attr):
  44. attr()
  45. #self.doc.save(self.filename_out)
  46.  
  47. def main():
  48. postproc = PostProcedure()
  49. postproc.run()
  50.  
  51.  
  52. if __name__ == "__main__":
  53. main()
Add Comment
Please, Sign In to add comment