Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import docx
- import datetime
- #import os
- import win32api
- import win32print
- def replace_text_in_docx(doc_replacements):
- for docx_path, replacements in doc_replacements.items():
- doc = docx.Document(docx_path)
- for paragraph in doc.paragraphs:
- for old_text, new_text in replacements.items():
- if old_text in paragraph.text:
- runs = paragraph.runs
- for run in runs:
- if old_text in run.text:
- new_run = run._element
- new_run.text = new_run.text.replace(old_text, new_text)
- doc.save(datetime.datetime.now().strftime("%d.%m.%Y") + "updated_" + docx_path)
- dictOfRecepientRequests = {
- "zdO.docx": {
- "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
- "$topicTitle": "jakis tam tytul",
- "$roadPlot": "567/8",
- "$city": "krakow",
- "$street": "ul. Gałgana"
- },
- "umigO.docx": {
- "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
- "$topicTitle": "orangik",
- "$roadPlot": "567/8, 8217372/23",
- "$city": "Krakow",
- "$street": "ul. Orange"
- },
- "pwikO.docx": {
- "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
- "$topicTitle": "pwik",
- "$roadPlot": "pwik, 876/32",
- "$city": "pwik",
- "$street": "pwik"
- },
- "orange.docx": {
- "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
- "$topicTitle": "orangik",
- "$roadPlot": "567/8, 8217372/23",
- "$city": "Krakow",
- "$street": "ul. Orange"
- },
- "tauron.docx": {
- "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
- "$topicTitle": "orangik",
- "$roadPlot": "567/8, 8217372/23",
- "$city": "Krakow",
- "$street": "ul. Orange"
- },
- "gddkiaK.docx": {
- "$date": datetime.datetime.now().strftime("%d.%m.%Y"),
- "$topicTitle": "orangik",
- "$roadPlot": "567/8, 8217372/23",
- "$city": "Krakow",
- "$street": "ul. Orange"
- },
- }
- listOfRecipientsChoices = ["1. ZD w krak", "2. UMIG krak", "3. PWIK w krak", "4. Orange"]
- doc_replacements = {}
- while True:
- 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: ")
- if chosenRecepients == "exit":
- break
- elif chosenRecepients == "lista":
- for list in listOfRecipientsChoices:
- print(list)
- else:
- if int(chosenRecepients) == 1:
- doc_replacements = {x:dictOfRecepientRequests[x] for x in dictOfRecepientRequests if x in ("zdO.docx")}
- replace_text_in_docx(doc_replacements)
- if int(chosenRecepients) == 2:
- doc_replacements = {x:dictOfRecepientRequests[x] for x in dictOfRecepientRequests if x in ("umigO.docx")}
- replace_text_in_docx(doc_replacements)
- if int(chosenRecepients) == 3:
- doc_replacements = {x:dictOfRecepientRequests[x] for x in dictOfRecepientRequests if x in ("pwikO.docx")}
- replace_text_in_docx(doc_replacements)
- #print(dictOfRecepientRequests["zdO.docx"]["$topicTitle"])
- #replace_text_in_docx(doc_replacements)
- #os.startfile("C:/bl.docx", "print")
- #win32api.ShellExecute ( 0, "print", "plikdowydruku.docx", '/d:"%s"' % win32print.GetDefaultPrinter (), ".", 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement