Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- from pathlib import Path
- import docx
- from python_docx_replace import docx_replace
- TEMPLATE_FILES_DIR = Path.cwd() / 'templates'
- OUTPUT_DIR = Path.cwd() / 'output'
- INPUT_PROMPT = "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: "
- TEMPLATES = {
- "1": {
- "help_name": "ZD w krak",
- "docx_template_name": "zdO.docx",
- "replacements": {
- "topicTitle": "jakis tam tytul",
- "roadPlot": "567/8",
- "city": "krakow",
- "street": "ul. Gałgana"
- }
- },
- "2": {
- "help_name": "UMIG krak",
- "docx_template_name": "umigO.docx",
- "replacements": {
- "topicTitle": "orangik",
- "roadPlot": "567/8, 8217372/23",
- "city": "Krakow",
- "street": "ul. Orange"
- }
- },
- "3": {
- "help_name": "PWIK w krak",
- "docx_template_name": "pwikO.docx",
- "replacements": {
- "topicTitle": "pwik",
- "roadPlot": "pwik, 876/32",
- "city": "pwik",
- "street": "pwik"
- }
- },
- "4": {
- "help_name": "Orange",
- "docx_template_name": "orange.docx",
- "replacements": {
- "topicTitle": "orangik",
- "roadPlot": "567/8, 8217372/23",
- "city": "Krakow",
- "street": "ul. Orange"
- }
- },
- }
- def create_docx_from_template(template: Path, output_dir: Path, replacements: dict[str, str]) -> Path:
- date = datetime.now().strftime("%d.%m.%Y")
- doc = docx.Document(template)
- docx_replace(doc, date=date, **replacements)
- output_path = output_dir / f"{date}updated_{template.name}"
- doc.save(output_path)
- return output_path
- def _print_help() -> None:
- for idx, data in TEMPLATES.items():
- print(f"{idx} - {data['help_name']}")
- def _generate_docx(choice: str) -> None:
- template_path = TEMPLATE_FILES_DIR / TEMPLATES[choice]["docx_template_name"]
- replacements = TEMPLATES[choice]["replacements"]
- output_path = create_docx_from_template(template_path, OUTPUT_DIR, replacements)
- print(f"Wniosek został wygenerowany: {output_path}")
- if __name__ == "__main__":
- while (choice := input(INPUT_PROMPT)) != "exit":
- if choice == "lista":
- _print_help()
- elif choice in TEMPLATES.keys():
- _generate_docx(choice)
- else:
- print(f"Nieznana komenda!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement