Generating tax reports with Python, Jinja, and Click

Every month I need to create a tax report using this ancient government website. Facing the choir, I found myself procrastinating every month, and this procrastination had been draining my energy. So I decided to face my daemons.

Encouraged by my last automation project, I discovered it is possible to semi-automate the choir. It turned out the website supports uploading the reports in XML format. It also turned out that my invoicing system provides REST API. I was onto something.

So I summoned my favorite tools for file generation—Python and Jinja —and added Click to the mix. The result is a CLI tool that extracts invoices from the API and generates the XML report.

While coding, I discovered new delightful things about Jinja and Click:

  • Jinja can raise an error during template generation if a variable is not set. This behavior is configured with
    Environment(...,undefined=jinja2.StrictUndefined)
    
  • Jinja can automatically escape content injected into XML files. So if my name would be "Marcel<", Jinja would generate <person name="{{ name }} /> as <person name="Marcel&lt;" />. Escaping is configured with
    Environment(...,autoescape=select_autoescape(['xml'])
    
  • Click can read options from environment variables. This is great: I added invoicing auth credentials to my shell startup script—in my case .zshrc—and now I don't have to think about them anymore.

I spent two evenings coding. Strictly from the time perspective, it was probably not an optimal investment. But when I generated my first report, I noticed a stark difference in my mood: I enjoyed the choir.


Would you like to connect? Subscribe via email or RSS , or follow me on Twitter!