From 24f33dc9bfe89e2e7e48c7b74ab4262531793246 Mon Sep 17 00:00:00 2001 From: tsuky_chen <3107760494@qq.com> Date: Sat, 30 Dec 2023 16:32:15 +0800 Subject: [PATCH] add eval libreoffice writer font & page break --- desktop_env/evaluators/metrics/__init__.py | 1 + desktop_env/evaluators/metrics/docs.py | 44 +++++++++++++++++++ .../2bd59342-0664-4ccb-ba87-79379096cc08.json | 7 +-- .../ecc2413d-8a48-416e-a3a2-d30106ca36cb.json | 11 ++++- .../f178a4a9-d090-4b56-bc4c-4b72a61a035d.json | 32 ++++++++++++-- 5 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 desktop_env/evaluators/metrics/docs.py diff --git a/desktop_env/evaluators/metrics/__init__.py b/desktop_env/evaluators/metrics/__init__.py index 73090bc..c881b27 100644 --- a/desktop_env/evaluators/metrics/__init__.py +++ b/desktop_env/evaluators/metrics/__init__.py @@ -1,2 +1,3 @@ from .table import compare_table, compare_with_sparklines, compare_with_charts from .table import check_sheet_list, check_xlsx_freeze +from .docs import find_default_font, contains_page_break \ No newline at end of file diff --git a/desktop_env/evaluators/metrics/docs.py b/desktop_env/evaluators/metrics/docs.py new file mode 100644 index 0000000..6399b31 --- /dev/null +++ b/desktop_env/evaluators/metrics/docs.py @@ -0,0 +1,44 @@ +import xml.etree.ElementTree as ET + +from docx import Document + +from lxml import etree + +def find_default_font(expected, config_file_path): + """Find the default font in LibreOffice Writer.""" + default_font = None + try: + tree = ET.parse(config_file_path) + root = tree.getroot() + + # Define the XML namespace used in the file + namespace = {'oor': 'http://openoffice.org/2001/registry'} + + # Search for the node containing the default font setting for LibreOffice Writer + for elem in root.findall('.//item[@oor:path="/org.openoffice.Office.Writer/DefaultFont"]', namespace): + for prop in elem.findall('.//prop[@oor:name="Standard"]', namespace): + for value in prop.findall('value', namespace): + default_font = value.text + except Exception as e: + print(f"Error: {e}") + return 1 if default_font == expected else 0 + + +def contains_page_break(docx_file): + doc = Document(docx_file) + + namespaces = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'} + + for paragraph in doc.paragraphs: + for run in paragraph.runs: + br_elems = run.element.findall('.//w:br', namespaces) + for br in br_elems: + if br is not None and '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}type' in br.attrib and br.attrib['{http://schemas.openxmlformats.org/wordprocessingml/2006/main}type'] == 'page': + return 1 + return 0 +# Replace 'your_document.docx' with the path to your document +# result = contains_page_break('your_document.docx') +# print(result) + +#config_path = "/home/[username]/.config/libreoffice/4/user/registrymodifications.xcu" +#print(find_default_font("Ani", config_path)) \ No newline at end of file diff --git a/evaluation_examples/examples/libreoffice_calc/2bd59342-0664-4ccb-ba87-79379096cc08.json b/evaluation_examples/examples/libreoffice_calc/2bd59342-0664-4ccb-ba87-79379096cc08.json index 4a211a2..d58c4b9 100644 --- a/evaluation_examples/examples/libreoffice_calc/2bd59342-0664-4ccb-ba87-79379096cc08.json +++ b/evaluation_examples/examples/libreoffice_calc/2bd59342-0664-4ccb-ba87-79379096cc08.json @@ -19,8 +19,9 @@ "type": "open", "parameters": { "path": "Desktop/OrderId_Month_Chart.xlsx" - ] - }, + } + } + ], "trajectory": "trajectories/2bd59342-0664-4ccb-ba87-79379096cc08", "related_apps": [ "libreoffice calc" @@ -38,4 +39,4 @@ "dest": "OrderId_Month_Chart.xlsx" } } -} +} \ No newline at end of file diff --git a/evaluation_examples/examples/libreoffice_writer/ecc2413d-8a48-416e-a3a2-d30106ca36cb.json b/evaluation_examples/examples/libreoffice_writer/ecc2413d-8a48-416e-a3a2-d30106ca36cb.json index 930a13d..6a802e7 100644 --- a/evaluation_examples/examples/libreoffice_writer/ecc2413d-8a48-416e-a3a2-d30106ca36cb.json +++ b/evaluation_examples/examples/libreoffice_writer/ecc2413d-8a48-416e-a3a2-d30106ca36cb.json @@ -8,5 +8,12 @@ "related_apps": [ "libreoffice_writer" ], - "evaluator": "evaluation_dir" -} + "evaluator": { + "func": "contains_page_break", + "result": { + "type": "vm_file", + "path": "Desktop/Insert_Blank_Page.docx", + "dest": "Insert_Blank_Page.docx" + } + } +} \ No newline at end of file diff --git a/evaluation_examples/examples/libreoffice_writer/f178a4a9-d090-4b56-bc4c-4b72a61a035d.json b/evaluation_examples/examples/libreoffice_writer/f178a4a9-d090-4b56-bc4c-4b72a61a035d.json index 2872625..62c7dee 100644 --- a/evaluation_examples/examples/libreoffice_writer/f178a4a9-d090-4b56-bc4c-4b72a61a035d.json +++ b/evaluation_examples/examples/libreoffice_writer/f178a4a9-d090-4b56-bc4c-4b72a61a035d.json @@ -3,10 +3,36 @@ "snapshot": "libreoffice_writer", "instruction": "Make Times New Roman the default Font", "source": "https://ask.libreoffice.org/t/how-do-i-make-times-new-roman-the-default-font-in-lo/64604", - "config": [], + "config": [ + { + "type": "download", + "parameters": { + "files": [ + { + "url": "https://drive.usercontent.google.com/download?id=1rwhniaClEkF8XFzdfaNUA6GmAiy4syMZ&export=download&authuser=0&confirm=t&uuid=6fdd5b04-85f4-45e1-ad74-368f8f2a82ab&at=APZUnTUP-JxPxLfNls6jXWghblQ5:1701766091851", + "path": "Desktop/Set_Default_Font.docx" + } + ] + } + }, + { + "type": "open", + "parameters": { + "path": "Desktop/Set_Default_Font.docx" + } + } + ], "trajectory": "trajectories/", "related_apps": [ "libreoffice_writer" ], - "evaluator": "evaluation_dir" -} + "evaluator": { + "func": "find_default_font", + "expected": "Times New Roman", + "result": { + "type": "vm_file", + "path": "/home/[your-username]/.config/libreoffice/4/user/registrymodifications.xcu", + "dest": "registrymodifications.xcu" + } + } +} \ No newline at end of file