From 6e6ef03bc991254b086d953a105abcffcc1266d3 Mon Sep 17 00:00:00 2001 From: David Chang Date: Tue, 2 Jan 2024 21:03:57 +0800 Subject: [PATCH 1/2] ver Jan2nd calc metrics are prapared by and large --- desktop_env/evaluators/metrics/__init__.py | 4 +- desktop_env/evaluators/metrics/libreoffice.py | 37 ++++++++++++++++ desktop_env/evaluators/metrics/pdf.py | 13 ++++++ desktop_env/evaluators/metrics/table.py | 30 +++++++++---- .../1334ca3e-f9e3-4db8-9ca7-b4c653be7d17.json | 2 +- .../4f07fbe9-70de-4927-a4d5-bb28bc12c52c.json | 42 ++++++++++++++++++ .../a01fbce3-2793-461f-ab86-43680ccbae25.json | 44 +++++++++++++++++++ .../aa3a8974-2e85-438b-b29e-a64df44deb4b.json | 44 +++++++++++++++++++ requirements.txt | 2 + 9 files changed, 208 insertions(+), 10 deletions(-) create mode 100644 desktop_env/evaluators/metrics/libreoffice.py create mode 100644 desktop_env/evaluators/metrics/pdf.py create mode 100644 evaluation_examples/examples/libreoffice_calc/4f07fbe9-70de-4927-a4d5-bb28bc12c52c.json create mode 100644 evaluation_examples/examples/libreoffice_calc/a01fbce3-2793-461f-ab86-43680ccbae25.json create mode 100644 evaluation_examples/examples/libreoffice_calc/aa3a8974-2e85-438b-b29e-a64df44deb4b.json diff --git a/desktop_env/evaluators/metrics/__init__.py b/desktop_env/evaluators/metrics/__init__.py index 69d65c5..5a3fd2a 100644 --- a/desktop_env/evaluators/metrics/__init__.py +++ b/desktop_env/evaluators/metrics/__init__.py @@ -1,3 +1,5 @@ from .table import compare_table -from .table import check_sheet_list, check_xlsx_freeze, check_zoom +from .table import check_sheet_list, check_xlsx_freeze, check_xlsx_zoom from .docs import find_default_font, contains_page_break, compare_docx_files +from .pdf import check_pdf_pages +from .libreoffice import check_libre_locale diff --git a/desktop_env/evaluators/metrics/libreoffice.py b/desktop_env/evaluators/metrics/libreoffice.py new file mode 100644 index 0000000..4ca07de --- /dev/null +++ b/desktop_env/evaluators/metrics/libreoffice.py @@ -0,0 +1,37 @@ +import lxml.cssselect +from lxml.etree import _Element as Element +import lxml.etree +import fnmatch + +from typing import Dict, List + +_libconf_namespaces = [("oor", "http://openoffice.org/2001/registry")] +_libconf_ns_mapping = dict(_libconf_namespaces) +_setup_locale_selector = lxml.cssselect.CSSSelector( 'item[oor|path$=L10N]>prop[oor|name=ooSetupSystemLocale]>value' + , namespaces=_libconf_ns_mapping + ) +_locale_selector = lxml.cssselect.CSSSelector( 'item[oor|path$=L10N]>prop[oor|name=ooLocale]>value' + , namespaces=_libconf_ns_mapping + ) +def check_libre_locale(config_file: str, rules: Dict[str, List[str]]) -> float: + config: Element = lxml.etree.parse(config_file).getroot() + setup_locale_setting: List[Element] = _setup_locale_selector(config) + locale_setting: List[Element] = _locale_selector(config) + + setup_locale_setting: str = setup_locale_setting[0].text\ + if len(setup_locale_setting)>0\ + else locale_setting[0].text + + return float( any( fnmatch.fnmatchcase(setup_locale_setting, ptn)\ + for ptn in rules["locale_set"] + ) + ) + +if __name__ == "__main__": + path1 = "../../任务数据/LibreOffice Calc/registrymodifications.ru.xcu" + print( check_libre_locale( path1, { "locale_set": [ "ru-*", "de-*", "fr-*" + , "pt-*", "es-*", "it-*" + ] + } + ) + ) diff --git a/desktop_env/evaluators/metrics/pdf.py b/desktop_env/evaluators/metrics/pdf.py new file mode 100644 index 0000000..51c79f3 --- /dev/null +++ b/desktop_env/evaluators/metrics/pdf.py @@ -0,0 +1,13 @@ +from pypdf import PdfReader +import operator + +from typing import Dict +from typing import Any + +def check_pdf_pages(pdf_file: str, rules: Dict[str, Any]) -> float: + reader = PdfReader(pdf_file) + nb_pages: int = len(reader.pages) + return float( getattr(operator, rules["relation"])( nb_pages + , rules["ref_value"] + ) + ) diff --git a/desktop_env/evaluators/metrics/table.py b/desktop_env/evaluators/metrics/table.py index f1a3eda..4cca1d7 100644 --- a/desktop_env/evaluators/metrics/table.py +++ b/desktop_env/evaluators/metrics/table.py @@ -110,7 +110,7 @@ def check_xlsx_freeze(result: str, rules: Dict[str, str]) -> float: worksheet: Worksheet = openpyxl.load_workbook(filename=result).active return float(worksheet.freeze_panes == rules["position"]) -def check_zoom(result: str, rules: Dict[str, Union[str, Number]]) -> float: +def check_xlsx_zoom(result: str, rules: Dict[str, Union[str, Number]]) -> float: worksheet = openpyxl.load_workbook(filename=result).active zoom_scale: Number = worksheet.sheet_view.zoomScale or 100. return float( getattr(operator, rules["relation"])( zoom_scale @@ -160,21 +160,31 @@ if __name__ == '__main__': #path1 = "../../任务数据/LibreOffice Calc/Represent_in_millions_billions_gold.xlsx" #path2 = "../../任务数据/LibreOffice Calc/Represent_in_millions_billions_gold3.xlsx" + #path1 = "../../任务数据/LibreOffice Calc/Set_Decimal_Separator_Dot.xlsx" + #path2 = "../../任务数据/LibreOffice Calc/Set_Decimal_Separator_Dot_gold.xlsx" #workbook1: Workbook = openpyxl.load_workbook(filename=path1) #worksheet1: Worksheet = workbook1.active -# #import itertools - #for col, r in itertools.product( ['A', 'B', 'C'] - #, range(1, 9) + #for col, r in itertools.product( ['A', 'B'] + #, range(1, 20) #): #position: str = "{:}{:d}".format(col, r) #print(worksheet1[position]) #print(worksheet1[position].value) #print(worksheet1[position].number_format) + #workbook2: Workbook = openpyxl.load_workbook(filename=path2) + #worksheet2: Worksheet = workbook2.active + #for col, r in itertools.product( ['A', 'B'] + #, range(1, 20) + #): + #position: str = "{:}{:d}".format(col, r) + #print(worksheet2[position]) + #print(worksheet2[position].value) + #print(worksheet2[position].number_format) #print(compare_table(path1, path2, features=["number_format"])) - path1 = "../../任务数据/LibreOffice Calc/Zoom_Out_Oversized_Cells_gold.xlsx" - path2 = "../../任务数据/LibreOffice Calc/Zoom_Out_Oversized_Cells.xlsx" + #path1 = "../../任务数据/LibreOffice Calc/Zoom_Out_Oversized_Cells_gold.xlsx" + #path2 = "../../任务数据/LibreOffice Calc/Zoom_Out_Oversized_Cells.xlsx" #workbook1: Workbook = openpyxl.load_workbook(filename=path1) #worksheet1: Worksheet = workbook1.active #print(worksheet1.sheet_view.zoomScale) @@ -187,5 +197,9 @@ if __name__ == '__main__': #): #path = os.path.join("../../任务数据/LibreOffice Calc/", wb) #print(wb, openpyxl.load_workbook(filename=path).active.sheet_view.zoomScale) - print(check_zoom(path1, {"relation": "lt", "ref_value": 100})) - print(check_zoom(path2, {"relation": "lt", "ref_value": 100})) + #print(check_zoom(path1, {"relation": "lt", "ref_value": 100})) + #print(check_zoom(path2, {"relation": "lt", "ref_value": 100})) + + path1 = "../../任务数据/LibreOffice Calc/Padding_Decimals_In_Formular_gold.xlsx" + data_frame: pd.DataFrame = pd.read_excel(path1) + print(data_frame) diff --git a/evaluation_examples/examples/libreoffice_calc/1334ca3e-f9e3-4db8-9ca7-b4c653be7d17.json b/evaluation_examples/examples/libreoffice_calc/1334ca3e-f9e3-4db8-9ca7-b4c653be7d17.json index 41c3cd2..9d6c8ec 100644 --- a/evaluation_examples/examples/libreoffice_calc/1334ca3e-f9e3-4db8-9ca7-b4c653be7d17.json +++ b/evaluation_examples/examples/libreoffice_calc/1334ca3e-f9e3-4db8-9ca7-b4c653be7d17.json @@ -27,7 +27,7 @@ "libreoffice_calc" ], "evaluator": { - "func": "check_zoom", + "func": "check_xlsx_zoom", "result": { "type": "vm_file", "path": "/home/david/Zoom_Out_Oversized_Cells.xlsx", diff --git a/evaluation_examples/examples/libreoffice_calc/4f07fbe9-70de-4927-a4d5-bb28bc12c52c.json b/evaluation_examples/examples/libreoffice_calc/4f07fbe9-70de-4927-a4d5-bb28bc12c52c.json new file mode 100644 index 0000000..14251e2 --- /dev/null +++ b/evaluation_examples/examples/libreoffice_calc/4f07fbe9-70de-4927-a4d5-bb28bc12c52c.json @@ -0,0 +1,42 @@ +{ + "id": "4f07fbe9-70de-4927-a4d5-bb28bc12c52c", + "snapshot": "libreoffice_calc", + "instruction": "Could you help me padding numbers to three decimals when used within formula?", + "source": "https://superuser.com/questions/1081048/libreoffice-calc-how-to-pad-number-to-fixed-decimals-when-used-within-formula", + "config": [ + { + "type": "download", + "parameters": { + "file": [ + { + "url": "https://drive.usercontent.google.com/download?id=1LHxVvEpLI7kp0Iiy8K74gwkoGiwcLeYP&export=download&authuser=0&confirm=t&uuid=690287ee-d413-46e7-9b01-c56c12e445ff&at=APZUnTVCSd_ajhMGWpEgLHiExfbf:1704199487820", + "path": "/home/david/Padding_Decimals_In_Formular.xlsx" + } + ] + } + }, + { + "type": "open", + "parameters": { + "path": "/home/david/Padding_Decimals_In_Formular.xlsx" + } + } + ], + "trajectory": "trajectories/4f07fbe9-70de-4927-a4d5-bb28bc12c52c", + "related_apps": [ + "libreoffice_calc" + ], + "evaluator": { + "func": "compare_table", + "result": { + "type": "vm_file", + "path": "/home/david/Padding_Decimals_In_Formular_gold.xlsx", + "dest": "Padding_Decimals_In_Formular.xlsx" + }, + "expected": { + "type": "cloud_file", + "path": "https://drive.usercontent.google.com/download?id=1DJTBic8_DREPhcau1GUkISfRm-L6xbBv&export=download&authuser=0&confirm=t&uuid=18e7d364-37fb-4e7f-baf1-9444ae5813ba&at=APZUnTXYuAaQ-Aa-2yqTn_2MXe0u:1704200380362", + "dest": "Padding_Decimals_In_Formular_gold.xlsx" + } + } +} diff --git a/evaluation_examples/examples/libreoffice_calc/a01fbce3-2793-461f-ab86-43680ccbae25.json b/evaluation_examples/examples/libreoffice_calc/a01fbce3-2793-461f-ab86-43680ccbae25.json new file mode 100644 index 0000000..d99f170 --- /dev/null +++ b/evaluation_examples/examples/libreoffice_calc/a01fbce3-2793-461f-ab86-43680ccbae25.json @@ -0,0 +1,44 @@ +{ + "id": "a01fbce3-2793-461f-ab86-43680ccbae25", + "snapshot": "libreoffice_calc", + "instruction": "Could you help me setting decimal separator as a dot (.)?", + "source": "https://superuser.com/questions/1250677/how-to-set-decimal-separator-in-libre-office-calc", + "config": [ + { + "type": "download", + "parameters": { + "file": [ + { + "url": "https://drive.usercontent.google.com/download?id=1uT0axjo9lwkKu6hYVnsAL2FCrdH0DLUv&export=download&authuser=0&confirm=t&uuid=e7da6304-9c7a-4862-8a30-9f2284b843da&at=APZUnTVNHThpAZJmF6IuPckFvslw:1704187618838", + "path": "/home/david/Set_Decimal_Separator_Dot.xlsx" + } + ] + } + } + ], + "trajectory": "trajectories/a01fbce3-2793-461f-ab86-43680ccbae25", + "related_apps": [ + "libreoffice_calc" + ], + "evaluator": { + "func": "check_libre_locale", + "result": { + "type": "vm_file", + "path": "/home/david/.config/libreoffice/4/user/registrymodifications.xcu", + "dest": "registrymodifications.xcu" + }, + "expected": { + "type": "rule", + "rules": { + "locale_set": [ + "ru-*", + "de-*", + "fr-*", + "pt-*", + "es-*", + "it-*" + ] + } + } + } +} diff --git a/evaluation_examples/examples/libreoffice_calc/aa3a8974-2e85-438b-b29e-a64df44deb4b.json b/evaluation_examples/examples/libreoffice_calc/aa3a8974-2e85-438b-b29e-a64df44deb4b.json new file mode 100644 index 0000000..ff3d191 --- /dev/null +++ b/evaluation_examples/examples/libreoffice_calc/aa3a8974-2e85-438b-b29e-a64df44deb4b.json @@ -0,0 +1,44 @@ +{ + "id": "aa3a8974-2e85-438b-b29e-a64df44deb4b", + "snapshot": "libreoffice_calc", + "instruction": "Could you help me resizing cells in the sheet so that they fit into just one page and export as pdf file?", + "source": "https://www.quora.com/Libre-Office-Calc-How-do-I-resize-all-cells-in-a-sheet-to-make-them-fit-to-1-page-for-printing-and-exporting-as-PDF", + "config": [ + { + "type": "download", + "parameters": { + "file": [ + { + "url": "https://drive.usercontent.google.com/download?id=1O4bw7jEsVdFGeGeSX8hDjsvIbozV38sd&export=download&authuser=0&confirm=t&uuid=b6ceade0-e9c3-47bf-8c40-fef77b5ea1f1&at=APZUnTUUYaEx0Y_lAESeK1DfQZw6:1704179724348", + "path": "/home/david/Resize_Cells_Fit_Page.xlsx" + } + ] + } + }, + { + "type": "open", + "parameters": { + "path": "/home/david/Resize_Cells_Fit_Page.xlsx" + } + } + ], + "trajectory": "trajectories/aa3a8974-2e85-438b-b29e-a64df44deb4b", + "related_apps": [ + "libreoffice_calc" + ], + "evaluator": { + "func": "check_pdf_pages", + "result": { + "type": "vm_file", + "path": "/home/david/Resize_Cells_Fit_Page.xlsx", + "dest": "Resize_Cells_Fit_Page.xlsx" + }, + "expected": { + "type": "rule", + "rules": { + "relation": "eq", + "ref_value": 1 + } + } + } +} diff --git a/requirements.txt b/requirements.txt index c5a7b86..14e3e9e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,3 +19,5 @@ lxml cssselect xmltodict openpyxl +python-docx +pypdf From f831aa93df229411b15c76b14f0c7b86d8c2cdd1 Mon Sep 17 00:00:00 2001 From: David Chang Date: Wed, 3 Jan 2024 22:42:19 +0800 Subject: [PATCH 2/2] ver Jan3rd exploring impress metrics --- desktop_env/evaluators/metrics/impress.py | 9 +++++++++ requirements.txt | 1 + 2 files changed, 10 insertions(+) create mode 100644 desktop_env/evaluators/metrics/impress.py diff --git a/desktop_env/evaluators/metrics/impress.py b/desktop_env/evaluators/metrics/impress.py new file mode 100644 index 0000000..641e3e4 --- /dev/null +++ b/desktop_env/evaluators/metrics/impress.py @@ -0,0 +1,9 @@ +from pptx import Presentation + +if __name__ == "__main__": + path1 = "../../任务数据/LibreOffice Impress/Change_Color_Slide_Number_gold_textbox.pptx" + presentation = Presentation(path1) + + for i, sl in enumerate(presentation.slides): + for j, sh in enumerate(sl.shapes): + print(i, j, sh, sh.name, sh.shape_type, sh.text) diff --git a/requirements.txt b/requirements.txt index 14e3e9e..5a3707b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,4 +20,5 @@ cssselect xmltodict openpyxl python-docx +python-pptx pypdf