first try
This commit is contained in:
0
src/invoice_generator/__init__.py
Normal file
0
src/invoice_generator/__init__.py
Normal file
38
src/invoice_generator/html_generator.py
Normal file
38
src/invoice_generator/html_generator.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from pathlib import Path
|
||||
from jinja2 import FileSystemLoader, TemplateNotFound, Environment
|
||||
|
||||
from xhtml2pdf import pisa
|
||||
|
||||
|
||||
class HtmlTemplate:
|
||||
def __init__(self, templates):
|
||||
self.template_name = 'invoice.html'
|
||||
if not Path(templates).exists():
|
||||
raise FileNotFoundError(f'Inivalid path to template files: {templates}')
|
||||
self.templates = templates
|
||||
|
||||
def prepare_template(self, invoice_data, envelope_data):
|
||||
try:
|
||||
loader = FileSystemLoader(self.templates)
|
||||
env = Environment(loader=loader)
|
||||
|
||||
template = env.get_template(self.template_name)
|
||||
return template.render(invoice=invoice_data, envelope=envelope_data)
|
||||
except TemplateNotFound:
|
||||
raise FileNotFoundError(f'Could not find template file: {self.template_name}')
|
||||
|
||||
@staticmethod
|
||||
def convert_html_to_pdf(source_html, output_filename):
|
||||
# open output file for writing (truncated binary)
|
||||
result_file = open(output_filename, "w+b")
|
||||
|
||||
# convert HTML to PDF
|
||||
pisa_status = pisa.CreatePDF(
|
||||
source_html, # the HTML to convert
|
||||
dest=result_file) # file handle to recieve result
|
||||
|
||||
# close output file
|
||||
result_file.close() # close output file
|
||||
|
||||
# return False on success and True on errors
|
||||
return pisa_status.err
|
||||
104
src/main.py
Normal file
104
src/main.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import argparse
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
|
||||
from invoice_generator import html_generator
|
||||
|
||||
|
||||
class DataObject:
|
||||
def __init__(self, **kwargs):
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
|
||||
def parse_yaml_file(file_path):
|
||||
with open(file_path, 'r') as file:
|
||||
return yaml.safe_load(file)
|
||||
|
||||
|
||||
def check_file(file_path):
|
||||
try_out_extensions = ['.yaml', '.yml']
|
||||
|
||||
for ext in try_out_extensions:
|
||||
if file_path.exists():
|
||||
return file_path
|
||||
file_path = file_path.with_suffix(ext)
|
||||
return None
|
||||
|
||||
|
||||
# Utility function
|
||||
|
||||
|
||||
def main():
|
||||
print('Simple invoice generator for freelancers and small businesses by Torsten Ueberschar')
|
||||
print()
|
||||
parser = argparse.ArgumentParser(description='Read invoice and envelope data from yaml file')
|
||||
parser.add_argument('-b', '--base', type=str, required=True, help='base directory for invoice and envelope files')
|
||||
parser.add_argument('-i', '--invoice', type=str, required=True, help='Invoice file name')
|
||||
parser.add_argument('-e', '--envelope', type=str, required=False, default='envelope.yaml',
|
||||
help='Envelope file name')
|
||||
parser.add_argument('-t', '--template', type=str, required=False, default='template',
|
||||
help='directory for template files')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
invoice_file = args.invoice
|
||||
envelope_file = args.envelope
|
||||
|
||||
print(f'Searching for invoice files in: {args.base}')
|
||||
|
||||
invoice_file = Path(args.base) / invoice_file
|
||||
envelope_file = Path(args.base) / envelope_file
|
||||
|
||||
invoice_file = check_file(invoice_file)
|
||||
envelope_file = check_file(envelope_file)
|
||||
|
||||
print(f'Found invoice file: {invoice_file}')
|
||||
print(f'Found envelope file: {envelope_file}')
|
||||
|
||||
if invoice_file is None:
|
||||
print('Error: No invoice file found')
|
||||
return
|
||||
if envelope_file is None:
|
||||
print('Error: No envelope file found')
|
||||
return
|
||||
|
||||
try:
|
||||
invoice = parse_yaml_file(invoice_file)
|
||||
envelope = parse_yaml_file(envelope_file)
|
||||
|
||||
print('Invoice data:')
|
||||
print(invoice)
|
||||
|
||||
print(envelope)
|
||||
|
||||
print('Envelope data:')
|
||||
envelope_data = DataObject(**envelope)
|
||||
print(envelope_data.__dict__)
|
||||
|
||||
print('Invoice data:')
|
||||
invoice_data = DataObject(**invoice)
|
||||
print(invoice_data.__dict__)
|
||||
|
||||
except FileNotFoundError as e:
|
||||
print(f'Error: {e}')
|
||||
return
|
||||
except yaml.YAMLError as e:
|
||||
print(f'Error: {e}')
|
||||
return
|
||||
except Exception as e:
|
||||
print(f'Error: {e}')
|
||||
return
|
||||
|
||||
generator = html_generator.HtmlTemplate(args.template)
|
||||
template = generator.prepare_template(invoice_data, envelope_data)
|
||||
|
||||
try:
|
||||
html_generator.HtmlTemplate.convert_html_to_pdf(template, 'invoice.pdf')
|
||||
except Exception as e:
|
||||
print(f'Error: {e}')
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user