Skip to content

Commit 70a8fd3

Browse files
author
HenryVisuri
committed
Xlsx jinja2 template implementations
1 parent bc2ba5b commit 70a8fd3

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

poetry.lock

Lines changed: 47 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/exporting/document.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from django.core.cache import cache
2121
from docx.shared import Mm
2222
from docxtpl import DocxTemplate, InlineImage, Listing, RichText
23+
from xlsxtpl.writerx import BookWriter
2324
from PIL import Image as PImage, UnidentifiedImageError
2425
from ..models import Attribute, ProjectPhase, ProjectAttributeFile, ProjectPhaseSectionAttribute
2526
from ..models.utils import create_identifier
@@ -262,10 +263,12 @@ def fetch_relevant_attributes(doc):
262263

263264
doc_type = get_file_type(document_template.file.path)
264265

266+
doc = None
267+
writer = None
265268
if doc_type == 'docx':
266269
doc = DocxTemplate(document_template.file)
267-
else:
268-
doc = None
270+
elif doc_type == 'xlsx':
271+
writer = BookWriter(document_template.file)
269272

270273
attribute_data_display = {}
271274
attribute_element_data = {}
@@ -519,6 +522,14 @@ def process_attribute(attr):
519522
doc.save(output)
520523
except Exception as exc:
521524
log.error('Error while rendering document', exc)
525+
elif doc_type == 'xlsx':
526+
try:
527+
writer.add_filter('distinct', distinct)
528+
writer.render_book([attribute_data_display])
529+
output = io.BytesIO()
530+
writer.save(output)
531+
except Exception as exc:
532+
log.error('Error while rendering document', exc)
522533
else:
523534
data = {
524535
'data': attribute_data_display,

projects/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,8 @@ def sanitize_attribute_data_filter_result(attributes, attribute_data):
999999

10001000
DOCUMENT_CONTENT_TYPES = {
10011001
'docx': "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
1002-
'pptx': "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"
1002+
'pptx': "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml",
1003+
'xlsx': "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
10031004
}
10041005

10051006

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ drf-extensions = "0.8.0"
4343
drf-oidc-auth = "3.0.0"
4444
psycopg2 = "2.9.9"
4545
docxtpl = "0.16.7"
46+
xltpl = "0.21"
4647
openpyxl = "3.1.2"
4748
python-pptx = "1.0.2"
4849
django-allauth = "65.11.2"

0 commit comments

Comments
 (0)