Advertisement
Guest User

Untitled

a guest
Jun 7th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. from datetime import datetime
  2. from pathlib import Path
  3.  
  4. import docx
  5. from python_docx_replace import docx_replace
  6.  
  7. TEMPLATE_FILES_DIR = Path.cwd() / 'templates'
  8. OUTPUT_DIR = Path.cwd() / 'output'
  9.  
  10. INPUT_PROMPT = "Podaj w postaci cyfry do kogo chesz wygenerować wniosek np 1 lub wpisz lista jeżeli " \
  11. "nie wiesz jaka cyfra odpowiada wnioskowi lub exit jeżeli chcesz wyjśc z programu: "
  12. TEMPLATES = {
  13. "1": {
  14. "help_name": "ZD w krak",
  15. "docx_template_name": "zdO.docx",
  16. "replacements": {
  17. "topicTitle": "jakis tam tytul",
  18. "roadPlot": "567/8",
  19. "city": "krakow",
  20. "street": "ul. Gałgana"
  21. }
  22. },
  23. "2": {
  24. "help_name": "UMIG krak",
  25. "docx_template_name": "umigO.docx",
  26. "replacements": {
  27. "topicTitle": "orangik",
  28. "roadPlot": "567/8, 8217372/23",
  29. "city": "Krakow",
  30. "street": "ul. Orange"
  31. }
  32. },
  33. "3": {
  34. "help_name": "PWIK w krak",
  35. "docx_template_name": "pwikO.docx",
  36. "replacements": {
  37. "topicTitle": "pwik",
  38. "roadPlot": "pwik, 876/32",
  39. "city": "pwik",
  40. "street": "pwik"
  41. }
  42. },
  43. "4": {
  44. "help_name": "Orange",
  45. "docx_template_name": "orange.docx",
  46. "replacements": {
  47. "topicTitle": "orangik",
  48. "roadPlot": "567/8, 8217372/23",
  49. "city": "Krakow",
  50. "street": "ul. Orange"
  51. }
  52. },
  53. }
  54.  
  55.  
  56. def create_docx_from_template(template: Path, output_dir: Path, replacements: dict[str, str]) -> Path:
  57. date = datetime.now().strftime("%d.%m.%Y")
  58.  
  59. doc = docx.Document(template)
  60. docx_replace(doc, date=date, **replacements)
  61.  
  62. output_path = output_dir / f"{date}updated_{template.name}"
  63. doc.save(output_path)
  64.  
  65. return output_path
  66.  
  67.  
  68. def _print_help() -> None:
  69. for idx, data in TEMPLATES.items():
  70. print(f"{idx} - {data['help_name']}")
  71.  
  72.  
  73. def _generate_docx(choice: str) -> None:
  74. template_path = TEMPLATE_FILES_DIR / TEMPLATES[choice]["docx_template_name"]
  75. replacements = TEMPLATES[choice]["replacements"]
  76. output_path = create_docx_from_template(template_path, OUTPUT_DIR, replacements)
  77. print(f"Wniosek został wygenerowany: {output_path}")
  78.  
  79.  
  80. if __name__ == "__main__":
  81. while (choice := input(INPUT_PROMPT)) != "exit":
  82.  
  83. if choice == "lista":
  84. _print_help()
  85. elif choice in TEMPLATES.keys():
  86. _generate_docx(choice)
  87. else:
  88. print(f"Nieznana komenda!")
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement