Advertisement
Guest User

Untitled

a guest
Jun 6th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. import docx
  2. import datetime
  3. #import os
  4. import win32api
  5. import win32print
  6.  
  7. def replace_text_in_docx(doc_replacements):
  8. for docx_path, replacements in doc_replacements.items():
  9. doc = docx.Document(docx_path)
  10.  
  11. for paragraph in doc.paragraphs:
  12. for old_text, new_text in replacements.items():
  13. if old_text in paragraph.text:
  14. runs = paragraph.runs
  15. for run in runs:
  16. if old_text in run.text:
  17. new_run = run._element
  18. new_run.text = new_run.text.replace(old_text, new_text)
  19.  
  20. doc.save(datetime.datetime.now().strftime("%d.%m.%Y") + "updated_" + docx_path)
  21.  
  22.  
  23. dictOfRecepientRequests = {
  24. "zdO.docx": {
  25. "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
  26. "$topicTitle": "jakis tam tytul",
  27. "$roadPlot": "567/8",
  28. "$city": "krakow",
  29. "$street": "ul. Gałgana"
  30. },
  31. "umigO.docx": {
  32. "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
  33. "$topicTitle": "orangik",
  34. "$roadPlot": "567/8, 8217372/23",
  35. "$city": "Krakow",
  36. "$street": "ul. Orange"
  37. },
  38. "pwikO.docx": {
  39. "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
  40. "$topicTitle": "pwik",
  41. "$roadPlot": "pwik, 876/32",
  42. "$city": "pwik",
  43. "$street": "pwik"
  44. },
  45. "orange.docx": {
  46. "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
  47. "$topicTitle": "orangik",
  48. "$roadPlot": "567/8, 8217372/23",
  49. "$city": "Krakow",
  50. "$street": "ul. Orange"
  51. },
  52. "tauron.docx": {
  53. "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
  54. "$topicTitle": "orangik",
  55. "$roadPlot": "567/8, 8217372/23",
  56. "$city": "Krakow",
  57. "$street": "ul. Orange"
  58. },
  59. "gddkiaK.docx": {
  60. "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
  61. "$topicTitle": "orangik",
  62. "$roadPlot": "567/8, 8217372/23",
  63. "$city": "Krakow",
  64. "$street": "ul. Orange"
  65. },
  66. }
  67.  
  68. listOfRecipientsChoices = ["1. ZD w krak", "2. UMIG krak", "3. PWIK w krak", "4. Orange"]
  69. doc_replacements = {}
  70.  
  71.  
  72. while True:
  73. chosenRecepients = input("Podaj w postaci cyfry do kogo chesz wygenerować wniosek np 1 lub wpisz lista jeżeli nie wiesz jaka cyfra odpowiada wnioskowi lub exit jeżeli chcesz wyjśc z programu: ")
  74. if chosenRecepients == "exit":
  75. break
  76. elif chosenRecepients == "lista":
  77. for list in listOfRecipientsChoices:
  78. print(list)
  79. else:
  80. if int(chosenRecepients) == 1:
  81. doc_replacements = {x:dictOfRecepientRequests[x] for x in dictOfRecepientRequests if x in ("zdO.docx")}
  82. replace_text_in_docx(doc_replacements)
  83. if int(chosenRecepients) == 2:
  84. doc_replacements = {x:dictOfRecepientRequests[x] for x in dictOfRecepientRequests if x in ("umigO.docx")}
  85. replace_text_in_docx(doc_replacements)
  86. if int(chosenRecepients) == 3:
  87. doc_replacements = {x:dictOfRecepientRequests[x] for x in dictOfRecepientRequests if x in ("pwikO.docx")}
  88. replace_text_in_docx(doc_replacements)
  89.  
  90.  
  91.  
  92. #print(dictOfRecepientRequests["zdO.docx"]["$topicTitle"])
  93.  
  94. #replace_text_in_docx(doc_replacements)
  95. #os.startfile("C:/bl.docx", "print")
  96.  
  97.  
  98.  
  99. #win32api.ShellExecute ( 0, "print", "plikdowydruku.docx", '/d:"%s"' % win32print.GetDefaultPrinter (), ".", 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement