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

v1.6.0 : var inside header or footer #33

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports/
/lotemplate/unittest/files/content/debug.odt
/lotemplate/unittest/files/content/debug.json
/lotemplate/unittest/files/content/*.unittest.odt
/lotemplate/unittest/files/content/*.unittest.pdf
/venv
/output*.*
.fontconfig/
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,10 @@ For trying to fix these problems, you can try:

<a name="versions"></a>Versions
-------------------------------

- v1.6.0 : 2024-04-11
- allow put variables inside headers and footers
- fix a bug when a variable is both inside the text content and inside a table (it should not arrive, but it is fixed)
- a new unit test system based on PDF converted to text in order to test contents that are not converted to text with a simple saveAs
- v1.5.2 : 2024-02-24 : Better README
- Rewrite for a betterdocker DockerFile without bug
- v1.5.1 : 2024-02-16 : Better README
Expand Down
28 changes: 21 additions & 7 deletions lotemplate/Statement/TextStatement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


class TextStatement:
text_regex = regex.compile(r'\$(\w+(\(((?:\\.|.)*?)\))?)')
text_regex_as_string = r'\$(\w+(\(((?:\\.|.)*?)\))?)'
text_regex = regex.compile(text_regex_as_string)
def __init__(self, text_string):
self.text_string = text_string

Expand All @@ -19,11 +20,21 @@ def scan_text(doc: XComponent) -> dict[str, dict[str, str]]:
:return: the scanned variables
"""

raw_string = doc.getText().getString()
matches = TextStatement.text_regex.finditer(raw_string)
search = doc.createSearchDescriptor()
search.SearchString = TextStatement.text_regex_as_string
search.SearchRegularExpression = True
search.SearchCaseSensitive = False
founded = doc.findAll(search)

simple_var_list = []
for x_found in founded:
text = x_found.getText()
cursor = text.createTextCursorByRange(x_found)
simple_var_list.append(cursor.String)

plain_vars = {}
for var in matches:
key_name = var[0][1:]
for var in simple_var_list:
key_name = var[1:]
# add to plain_vars if it doesn't matche ForStatement.foritem_regex
if not re.search(ForStatement.forindex_regex, key_name, re.IGNORECASE):
plain_vars[key_name] = {'type': 'text', 'value': ''}
Expand All @@ -38,8 +49,11 @@ def scan_text(doc: XComponent) -> dict[str, dict[str, str]]:
text_fields_vars = (text_fields_vars |
{var.group(0)[1:]: {'type': 'text', 'value': ''} for var in matches})

for var in TableStatement.scan_table(doc, get_list=True):
if '$' + var in plain_vars:
table_var_list = TableStatement.scan_table(doc, get_list=True)
for var in table_var_list:
if var.startswith("$"):
var = var[1:]
if var in plain_vars:
del plain_vars[var]

for var in ForStatement.scan_for(doc):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Can we change the following var my replaced text in the header ?
This document test the replacement of a variable in the header.
And in the footer the var “my replaced text” is still ther
6 changes: 6 additions & 0 deletions lotemplate/unittest/files/content/text_var_in_header.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"my_var": {
"type": "text",
"value": "my replaced text"
}
}
Binary file not shown.
Binary file not shown.
31 changes: 25 additions & 6 deletions lotemplate/unittest/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lotemplate as ot
from time import sleep
import subprocess
from pypdf import PdfReader

subprocess.call(f'soffice "--accept=socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" &', shell=True)
sleep(2)
Expand All @@ -28,7 +29,10 @@ def to_data(file: str):
return ot.convert_to_datas_template(file_to_dict(file))


def compare_files(name: str):
def compare_files(name: str, format: str = 'txt'):
if format not in ['txt', 'pdf']:
return False

base_path = 'lotemplate/unittest/files/content'

def get_filename(ext: str):
Expand All @@ -50,17 +54,29 @@ def get_filename(ext: str):
temp.search_error(to_data(get_filename('json')))
temp.fill(file_to_dict(get_filename('json')))

if os.path.isfile(get_filename('unittest.txt')):
os.remove(get_filename('unittest.txt'))
temp.export(get_filename('unittest.txt'), True)
if os.path.isfile(get_filename('unittest.'+format)):
os.remove(get_filename('unittest.'+format))
temp.export(get_filename('unittest.'+format), True)
# temp.close()
if os.path.isfile(get_filename('unittest.odt')):
os.remove(get_filename('unittest.odt'))
temp.export(get_filename('unittest.odt'), True)
temp.close()

# The PDF format is used to test some documents with headers or footers that are not supported by the text saveAs from
# LibreOffice. The PDF is then converted to text to compare with the expected text.
if format == 'pdf':
# convert to text
reader = PdfReader(get_filename('unittest.pdf'))
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
if os.path.isfile(get_filename('unittest.txt')):
os.remove(get_filename('unittest.txt'))
with open(get_filename('unittest.txt'), 'w') as f:
f.write(text)

response = filecmp.cmp(get_filename('unittest.txt'), get_filename('expected.txt'))
# if os.path.isfile(get_filename('unittest.txt')):
# os.remove(get_filename('unittest.txt'))
return response


Expand Down Expand Up @@ -112,5 +128,8 @@ def test_image(self):
def test_counter(self):
self.assertTrue(compare_files('counter'))

def test_text_var_in_header(self):
self.assertTrue(compare_files('text_var_in_header', 'pdf'))

def test_debug(self):
self.assertTrue(compare_files('debug'))
7 changes: 7 additions & 0 deletions lotemplate/unittest/test_template_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def test_format(self):
(doc := ot.Template("lotemplate/unittest/files/templates/text_vars.odt", cnx, False)).scan())
doc.close()

def test_text_var_in_header(self):
self.assertEqual(
{"my_var": {"type": "text", "value": ""}},
(doc := ot.Template("lotemplate/unittest/files/templates/text_var_in_header.odt", cnx, False)).scan()
)
doc.close()

def test_static_table(self):
self.assertEqual(
{"var1": {"type": "text", "value": ""}, "var2": {"type": "text", "value": ""}},
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gunicorn~=20.1.0
Werkzeug~=2.2.2
sorcery~=0.2.1
regex~=2022.10.31
pypdf~=4.2.0
Loading