Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JSON data + RML template = PDF report

JSON data + RML template = PDF report

Talk: https://www.youtube.com/watch?v=yf34NCtdxVI

The main problem with reports generated in Python is how to separate the content from the style using ReportLab library, because all informations should be saved in a single source file that, by example, is impossible to understand for your graphic designer.

So the solution: just modularizes all components you need and identify simple container formats for your data input (JSON) and document template (ReportLab RML).

Now with the power of Genshi and XInclude we will create dynamic templates that include specific snippets (e.g., to generate on the fly a decent graph with matplotlib or cairoplot to fill some lacks of ReportLab) and we will detach the stylesheet from the template structure.

And at the end you can also have the internationalization service in the PDF report generation!

Stefano Cotta Ramusino

June 23, 2011
Tweet

More Decks by Stefano Cotta Ramusino

Other Decks in Programming

Transcript

  1. 2011.06.23 2011.06.23 Slide Slide 2 2 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Target Target Request: create a PDF starting from data formatted with a “flexible” layout Requirements: limited disk space (no LaTeX)
  2. 2011.06.23 2011.06.23 Slide Slide 3 3 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 What's “flexible”? What's “flexible”? No hope: no one knows when layout reaches the final version, if it ever reaches one Error-free: handle some missing input data Dynamic: graphics generated on the fly
  3. 2011.06.23 2011.06.23 Slide Slide 4 4 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Python PDF Report Engines Python PDF Report Engines ReportLab rst2pdf pisa (xhtml2pdf) Sphinx with rst2pdf and rst2latex PyReport with LaTeX matplotlib (PdfPages method) pod with LibreOffice in server mode
  4. 2011.06.23 2011.06.23 Slide Slide 5 5 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 ReportLab ReportLab Canvas matryoshka mode Platypus (Page Layout and Typography Using Scripts) RML (Report Markup Language)
  5. 2011.06.23 2011.06.23 Slide Slide 6 6 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 rml2pdf rml2pdf ReportLab PLUS (commercial) z3c.rml (Zope Community)
  6. 2011.06.23 2011.06.23 Slide Slide 7 7 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 RML RML Learning path: RML For Idiots RML Samples RML User Guide z3c.RML Reference
  7. 2011.06.23 2011.06.23 Slide Slide 8 8 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 RML right now RML right now odt2rml based on sxw2rml (Tiny/OpenERP)
  8. 2011.06.23 2011.06.23 Slide Slide 9 9 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 pyjon.reports pyjon.reports Create PDF in Python using Genshi and RML by Jonathan Schemoul
  9. 2011.06.23 2011.06.23 Slide Slide 10 10 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Genshi's power Genshi's power template language XInclude support
  10. 2011.06.23 2011.06.23 Slide Slide 11 11 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Ready to use Ready to use pip install pyjon.reports pil installs also: ReportLab z3c.rml Genshi pyPdf
  11. 2011.06.23 2011.06.23 Slide Slide 12 12 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Data input format Data input format JSON included in Python 2.6+ YAML need PyYAML or PySick Python itself
  12. 2011.06.23 2011.06.23 Slide Slide 13 13 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Working model Working model default data languages JSON report.rml story templates stylesheets report RML PDF
  13. 2011.06.23 2011.06.23 Slide Slide 14 14 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Main code Main code
  14. 2011.06.23 2011.06.23 Slide Slide 15 15 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 XInclude XInclude
  15. 2011.06.23 2011.06.23 Slide Slide 16 16 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 JSON loader JSON loader
  16. 2011.06.23 2011.06.23 Slide Slide 17 17 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 JSON example JSON example
  17. 2011.06.23 2011.06.23 Slide Slide 18 18 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Real RML content Real RML content
  18. 2011.06.23 2011.06.23 Slide Slide 19 19 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Dynamic RML content Dynamic RML content
  19. 2011.06.23 2011.06.23 Slide Slide 20 20 Stefano Cotta Ramusino <

    Stefano Cotta Ramusino <[email protected] [email protected]> EUROPYTHON 2011 > EUROPYTHON 2011 Questions and answers Questions and answers identi.ca/whitone www.whitone.tk [email protected]