Skip to content

Commit 3109780

Browse files
committed
Handle images with MsOffice by permitting using Description field for
Variable. Add Unit test for images
1 parent 33c02cf commit 3109780

File tree

8 files changed

+68
-12
lines changed

8 files changed

+68
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ Limitation : Html is not interpreted into "shape content". For example for a tex
346346

347347
### image variables
348348

349-
Add any image in the document, and put in the title of the alt text of the image
349+
Add any image in the document, and put in the title of the alt text of the image (If your are using MsOffice you can use the Description field)
350350
(properties) '$' followed by the desired name ('$image' for example to add the image 'image')
351351

352352
### dynamic arrays

lotemplate/Statement/ImageStatement.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from lotemplate.utils import get_file_url, is_network_based
77
from com.sun.star.awt import Size
88

9-
109
class ImageStatement:
1110
image_regex = regex.compile(r'\$\w+')
1211

@@ -17,12 +16,13 @@ def scan_image(doc: XComponent) -> dict[str, dict[str, str]]:
1716
:param doc: the document to scan
1817
:return: the scanned variables
1918
"""
20-
21-
return {
22-
elem.LinkDisplayName[1:]: {'type': 'image', 'value': ''}
23-
for elem in doc.getGraphicObjects()
24-
if ImageStatement.image_regex.fullmatch(elem.LinkDisplayName)
25-
}
19+
imgs = {}
20+
for img in doc.getGraphicObjects():
21+
if ImageStatement.image_regex.fullmatch(img.LinkDisplayName):
22+
imgs[ImageStatement.image_regex.match(img.LinkDisplayName).group(0)[1:]] = {'type': 'image', 'value': ''}
23+
elif ImageStatement.image_regex.fullmatch(img.Description):
24+
imgs[ImageStatement.image_regex.match(img.Description).group(0)[1:]] = {'type': 'image', 'value': ''}
25+
return imgs
2626

2727
def image_fill(doc: XComponent, graphic_provider, variable: str, path: str, should_resize=True) -> None:
2828
"""
@@ -40,7 +40,7 @@ def image_fill(doc: XComponent, graphic_provider, variable: str, path: str, shou
4040
return
4141

4242
for graphic_object in doc.getGraphicObjects():
43-
if graphic_object.LinkDisplayName != variable:
43+
if graphic_object.LinkDisplayName != variable and graphic_object.Description != variable :
4444
continue
4545

4646
new_image = graphic_provider.queryGraphic((PropertyValue('URL', 0, get_file_url(path), 0),))
@@ -54,4 +54,3 @@ def image_fill(doc: XComponent, graphic_provider, variable: str, path: str, shou
5454
graphic_object.setSize(new_size)
5555

5656
graphic_object.Graphic = new_image
57-
31.3 KB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Test": {
3+
"type": "text",
4+
"value": "OK OK"
5+
},
6+
"var2": {
7+
"type": "image",
8+
"value": "lotemplate/unittest/files/jsons/Yami_Yugi.png"
9+
},
10+
"var1": {
11+
"type": "image",
12+
"value": "lotemplate/unittest/files/jsons/Yami_Yugi.png"
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"image": {"type": "image", "value": "lotemplate/unittest/files/jsons/Yami_Yugi.png"}}
61.1 KB
Binary file not shown.

lotemplate/unittest/test_comparaison.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import unittest
66
import lotemplate as ot
77
from lotemplate.unittest.test_function import to_data
8-
8+
from lotemplate.unittest.test_function import compare_image
99
cnx=ot.start_multi_office()
1010

1111

@@ -75,7 +75,12 @@ def test_invalid_unknown_variable(self):
7575
Check that there is no exception anymore where there is an unknown variable in the json
7676
"""
7777
self.temp.search_error(to_data("lotemplate/unittest/files/comparaison/img_vars_invalid_other_image.json"))
78+
def test_image_odt(self):
79+
self.assertTrue(compare_image('image_odt',cnx))
80+
temp.close()
7881

82+
def test_image_doc(self):
83+
self.assertTrue(compare_image('image_docx',cnx))
7984
temp.close()
8085

8186

lotemplate/unittest/test_function.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import json
1010
from pypdf import PdfReader
11-
11+
import glob
1212

1313

1414
def file_to_dict(file_path: str) -> dict:
@@ -20,6 +20,39 @@ def file_to_dict(file_path: str) -> dict:
2020
def to_data(file: str):
2121
return ot.convert_to_datas_template(file_to_dict(file))
2222

23+
def compare_image(name: str, cnx) :
24+
base_path = 'lotemplate/unittest/files/content'
25+
def get_filename(ext: str):
26+
return base_path + '/' + name + '.' + ext
27+
28+
temp = None
29+
if os.path.isfile(get_filename('ods')):
30+
temp = ot.TemplateFromExt(get_filename('ods'), ot.randomConnexion(cnx), True)
31+
if os.path.isfile(get_filename('odt')):
32+
temp = ot.TemplateFromExt(get_filename('odt'), ot.randomConnexion(cnx), True)
33+
if os.path.isfile(get_filename('docx')):
34+
temp = ot.TemplateFromExt(get_filename('docx'), ot.randomConnexion(cnx), True)
35+
36+
if temp is None:
37+
if name == 'debug':
38+
return True
39+
else:
40+
raise FileNotFoundError('No file found for ' + name)
41+
42+
temp.scan()
43+
temp.search_error(to_data(get_filename('json')))
44+
temp.fill(file_to_dict(get_filename('json')))
45+
if os.path.isfile(get_filename('unittest.html')):
46+
os.remove(get_filename('unittest.html'))
47+
temp.export(name+'.unittest.html',base_path, True)
48+
temp.close()
49+
response = filecmp.cmp(
50+
"lotemplate/unittest/files/jsons/Yami_Yugi.png",
51+
list(glob.iglob(base_path + '/' + name +'.unittest_html*.png'))[0],
52+
shallow=False
53+
)
54+
return response
55+
2356

2457
def compare_files_html(name: str, cnx ):
2558

@@ -31,6 +64,10 @@ def get_filename(ext: str):
3164
temp = None
3265
if os.path.isfile(get_filename('ods')):
3366
temp = ot.TemplateFromExt(get_filename('ods'), ot.randomConnexion(cnx), True)
67+
if os.path.isfile(get_filename('odt')):
68+
temp = ot.TemplateFromExt(get_filename('odt'), ot.randomConnexion(cnx), True)
69+
if os.path.isfile(get_filename('docx')):
70+
temp = ot.TemplateFromExt(get_filename('docx'), ot.randomConnexion(cnx), True)
3471

3572
if temp is None:
3673
if name == 'debug':

0 commit comments

Comments
 (0)