Skip to content

Instantly share code, notes, and snippets.

@Sven-Bo
Created April 24, 2023 17:45
Show Gist options
  • Save Sven-Bo/bfaff775394579d4915aab9c43700633 to your computer and use it in GitHub Desktop.
Save Sven-Bo/bfaff775394579d4915aab9c43700633 to your computer and use it in GitHub Desktop.
Custom Contract Generator - An enhanced version of the original script, allowing users to generate and save vendor contracts in a specified folder using PySimpleGUI and docxtpl
# Author: Sven Bosau
# YouTube: https://youtube.com/@codingisfun
# This script is a modified version of the original code from the following video:
# https://github.com/Sven-Bo/word-entry-form/blob/master/2.Step%20-%20Create%20GUI/Vendor_Contract_Example/vendor-contract-gui.py
# The modification allows the user to save generated documents in a specified folder.
import datetime
from pathlib import Path
import PySimpleGUI as sg
from docxtpl import DocxTemplate
document_path = Path(__file__).parent / "vendor-contract.docx"
doc = DocxTemplate(document_path)
today = datetime.datetime.today()
today_in_one_week = today + datetime.timedelta(days=7)
layout = [
[sg.Text("Client name:"), sg.Input(key="CLIENT", do_not_clear=False)],
[sg.Text("Vendor name:"), sg.Input(key="VENDOR", do_not_clear=False)],
[sg.Text("Amount:"), sg.Input(key="AMOUNT", do_not_clear=False)],
[sg.Text("Description1:"), sg.Input(key="LINE1", do_not_clear=False)],
[sg.Text("Description2:"), sg.Input(key="LINE2", do_not_clear=False)],
[sg.Button("Create Contract"), sg.Exit()],
]
window = sg.Window("Contract Generator", layout, element_justification="right")
output_folder = Path(__file__).parent / "output"
output_folder.mkdir(exist_ok=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Exit":
break
if event == "Create Contract":
# ... (same as before)
# Render the template, save new word document & inform user
doc.render(values)
output_path = output_folder / f"{values['VENDOR']}-contract.docx"
doc.save(output_path)
sg.popup("File saved", f"File has been saved here: {output_path}")
window.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment