gnucash/bindings/python/example_scripts/gncinvoice_jinja.py

183 lines
5.4 KiB
Python
Raw Normal View History

2018-09-02 09:25:04 -05:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##@file
# @brief exports an invoice from gnucash using a template file, see \ref py_invoice_export
# @ingroup python_bindings_examples
# @author Christoph Holtermann (c.holtermann (at) gmx.de)
# @date 2014-11
#
# @details
# Input is a template file that will be filled with information from
# gnucash Invoices. Jinja2 templating engine ist used. Templates can
# be Latex, Html or anything.
#
2014-11-12 06:28:25 -06:00
# Example templates for german invoices:
# - Invoice.tex.tmpl
# - Invoice_2.tex.tmpl
#
# This is a sequel to latex_invoices.py that exported to a lco file
# to be imported into a LaTeX letter.
# The approach used here is not as dependent on external files and
# more modular as it allows to use arbitrary templates
#
# Doxygen docs:
# - https://code.gnucash.org/docs/MAINT or MASTER
# - see page \ref py_invoice_export
#
# Questions / Issues:
2014-11-12 06:28:25 -06:00
# - How much logic in the template, how much preprocessing in this file ?
# - Internationalization - currencies, formatting of numbers
# - Providing data of gnucash owner
try:
2014-11-11 14:35:15 -06:00
import locale
import sys
import getopt
import gnucash
import str_methods
import jinja2
from gncinvoicefkt import *
except ImportError as import_error:
2018-03-22 17:08:22 -05:00
print("Problem importing modules.")
print(import_error)
sys.exit(2)
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
# default values
prog_name = argv[0]
ignore_lock = True
filename_template = None
filename_output = None
no_output = False
list_invoices = False
invoice_number = None
invoice_id = None
try:
opts, args = getopt.getopt(argv[1:], "fhlI:t:o:", ["help"])
2018-09-02 09:25:04 -05:00
except getopt.error as msg:
raise Usage(msg)
for opt in opts:
if opt[0] in ["-f"]:
2018-03-22 17:08:22 -05:00
print("ignoring lock")
ignore_lock = True
if opt[0] in ["-h","--help"]:
raise Usage("Help:")
if opt[0] in ["-I"]:
invoice_id = opt[1]
2018-03-22 17:08:22 -05:00
print("using invoice ID '" + str(invoice_id) + "'.")
if opt[0] in ["-o"]:
filename_output = opt[1]
2018-03-22 17:08:22 -05:00
print("using output file", filename_output)
if opt[0] in ["-t"]:
filename_template = opt[1]
2018-03-22 17:08:22 -05:00
print("using template file", filename_template)
if opt[0] in ["-l"]:
list_invoices = True
2018-03-22 17:08:22 -05:00
print("listing invoices")
# Check for correct input
if len(args)>1:
2018-03-22 17:08:22 -05:00
print("opts:",opts,"args:",args)
raise Usage("Only one input possible !")
if len(args)==0:
raise Usage("No input given !")
input_url = args[0]
# Check for correct template
if not filename_template:
no_output = True
if not list_invoices:
raise Usage("No template given !")
# Check for output file
if not filename_output:
if filename_template:
filename_output = filename_template + ".out"
2018-03-22 17:08:22 -05:00
print("no output filename given, will be:", filename_output)
2018-09-02 09:25:04 -05:00
except Usage as err:
if err.msg == "Help:":
retcode=0
else:
2018-09-02 09:25:04 -05:00
print("Error:", err.msg, file=sys.stderr)
print("for help use --help", file=sys.stderr)
retcode=2
2018-03-22 17:08:22 -05:00
print()
print("Usage:")
print()
print("Invoke with",prog_name,"gnucash_url.")
print("where input is")
print(" filename")
print("or file://filename")
print("or mysql://user:password@host/databasename")
print()
print("-f force open = ignore lock")
print("-l list all invoices")
print("-h or --help for this help")
print("-I ID use invoice ID")
print("-t filename use filename as template file")
print("-o filename use filename as output file")
return retcode
# Try to open the given input
try:
2018-03-22 17:08:22 -05:00
print("Opening", input_url, ".")
session = gnucash.Session(input_url, ignore_lock=ignore_lock)
except Exception as exception:
2018-03-22 17:08:22 -05:00
print("Problem opening input.")
print(exception)
return 2
book = session.book
root_account = book.get_root_account()
comm_table = book.get_table()
EUR = comm_table.lookup("CURRENCY", "EUR")
invoice_list = get_all_invoices(book)
if list_invoices:
for number,invoice in enumerate(invoice_list):
2018-03-22 17:08:22 -05:00
print(str(number)+")")
print(invoice)
if not (no_output):
if invoice_id:
invoice = book.InvoiceLookupByID(invoice_id)
if not invoice:
2018-03-22 17:08:22 -05:00
print("ID not found.")
return 2
if invoice_number:
invoice = invoice_list[invoice_number]
2018-03-22 17:08:22 -05:00
print("Using the following invoice:")
print(invoice)
loader = jinja2.FileSystemLoader('.')
env = jinja2.Environment(loader=loader)
template = env.get_template(filename_template)
#import IPython
#IPython.embed()
2014-11-11 14:35:15 -06:00
output = template.render(invoice=invoice, locale=locale)
2018-03-22 17:08:22 -05:00
print("Writing output", filename_output, ".")
with open(filename_output, 'w') as f:
f.write(output.encode('utf-8'))
if __name__ == "__main__":
sys.exit(main())