docx utility
This commit is contained in:
parent
e8dde759ad
commit
59510819e7
45
atst/utils/docx.py
Normal file
45
atst/utils/docx.py
Normal file
@ -0,0 +1,45 @@
|
||||
import os
|
||||
from io import BytesIO
|
||||
from zipfile import ZipFile
|
||||
from flask import render_template, current_app as app
|
||||
|
||||
|
||||
class Docx:
|
||||
DOCUMENT_FILE = "word/document.xml"
|
||||
|
||||
@classmethod
|
||||
def _template_path(cls, docx_file):
|
||||
return os.path.join(app.root_path, "..", "templates", docx_file)
|
||||
|
||||
@classmethod
|
||||
def _template(cls, docx_file):
|
||||
return ZipFile(Docx._template_path(docx_file), mode="r")
|
||||
|
||||
@classmethod
|
||||
def _write(cls, docx_template, docx_file, document):
|
||||
with docx_template as template:
|
||||
for item in template.infolist():
|
||||
if item.filename != Docx.DOCUMENT_FILE:
|
||||
content = template.read(item.filename).decode()
|
||||
else:
|
||||
content = document
|
||||
|
||||
docx_file.writestr(item, content)
|
||||
|
||||
return docx_file
|
||||
|
||||
@classmethod
|
||||
def render(
|
||||
cls,
|
||||
doc_template="docx/document.xml",
|
||||
file_template="docx/template.docx",
|
||||
**args,
|
||||
):
|
||||
document = render_template(doc_template, **args)
|
||||
byte_str = BytesIO()
|
||||
docx_file = ZipFile(byte_str, mode="w")
|
||||
docx_template = Docx._template(file_template)
|
||||
Docx._write(docx_template, docx_file, document)
|
||||
docx_file.close()
|
||||
byte_str.seek(0)
|
||||
return byte_str.read()
|
12
templates/docx/document.xml
Normal file
12
templates/docx/document.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
|
||||
<w:body>
|
||||
{% for key,val in data.items() %}
|
||||
<w:p>
|
||||
<w:r>
|
||||
<w:t>{{ key }}: {{ val }}</w:t>
|
||||
</w:r>
|
||||
</w:p>
|
||||
{% endfor %}
|
||||
</w:body>
|
||||
</w:document>
|
BIN
templates/docx/template.docx
Normal file
BIN
templates/docx/template.docx
Normal file
Binary file not shown.
12
tests/utils/test_docx.py
Normal file
12
tests/utils/test_docx.py
Normal file
@ -0,0 +1,12 @@
|
||||
from io import BytesIO
|
||||
from zipfile import ZipFile
|
||||
|
||||
from atst.utils.docx import Docx
|
||||
|
||||
|
||||
def test_render_docx():
|
||||
data = {"droid_class": "R2"}
|
||||
docx_file = Docx.render(data=data)
|
||||
zip_ = ZipFile(BytesIO(docx_file), mode="r")
|
||||
document = zip_.read(Docx.DOCUMENT_FILE)
|
||||
assert b"droid_class: R2" in document
|
Loading…
x
Reference in New Issue
Block a user