Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiline strings #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions app/templater/lib/templater/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import csv
import xlrd
from docxtpl import DocxTemplate
from docxtpl import DocxTemplate, RichText
from jinja2 import Environment, Template, meta
from .custom_exceptions import TemplateTypeNotSupported, OutputNameTemplateSyntax
from .custom_odt_renderer import CustomOdtRenderer
Expand All @@ -20,23 +20,30 @@ def __init__(self):

class TemplateRenderer(object):

MULTILINE_PREFIX = "&^<\\multiline>"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add examples of templates and tables for this feature and update README.md with its description


def __init__(self):
self.contexts = []
self.fieldnames = []
self.raw_table = []

def _preprocess_value(self, value):
if value.startswith(self.MULTILINE_PREFIX):
value = RichText(value[len(self.MULTILINE_PREFIX):])
return value

# Load data from csv file
def load_csv(self, csvfile, delimiter):
# open csv file and prepare context array
lines = csvfile.read().decode("utf-8-sig").splitlines()
reader = csv.DictReader(lines, delimiter=delimiter)
lines = csvfile.read().decode("utf-8-sig")
reader = csv.DictReader(io.StringIO(lines), delimiter=delimiter)
self.fieldnames = reader.fieldnames
self.raw_table.append(self.fieldnames)
for row in reader:
context = {}
r = []
for key, value in row.items():
context[key] = value
context[key] = self._preprocess_value(value)
r.append(value)
self.contexts.append(context)
self.raw_table.append(r)
Expand All @@ -52,7 +59,7 @@ def load_excel(self, excelfile):
r = []
for col in range(sh.ncols):
value = str(sh.cell_value(rowx=row, colx=col))
context[self.fieldnames[col]] = value
context[self.fieldnames[col]] = self._preprocess_value(value)
r.append(value)
self.contexts.append(context)
self.raw_table.append(r)
Expand Down