ver Dec26thv2

implemented _load_charts and compare_with_charts according to codes in
openpyxl
This commit is contained in:
David Chang
2023-12-26 20:59:19 +08:00
parent a6b6022ecb
commit fe0a59583a
3 changed files with 137 additions and 16 deletions

View File

@@ -6,8 +6,9 @@ from lxml.etree import _Element
import xmltodict
#import pylightxl
import openpyxl
#from openpyxl import Workbook
from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet
from openpyxl.chart._chart import ChartBase
from typing import Dict, List
from typing import Any
@@ -69,6 +70,59 @@ def compare_with_sparklines(actual: str, expected: str) -> float:
return float(normal_content_metric and sparkline_metric)
def _load_charts(xlsx_file: str) -> Dict[str, Any]:
"""
Args:
xlsx_file (str): path to xlsx
Returns:
Dict[str, Any]: information of charts
"""
workbook: Workbook = openpyxl.load_workbook(filename=xlsx_file)
worksheet: Worksheet = workbook.active
charts: List[ChartBase] = worksheet._charts
chart_set: Dict[str, Any] = {}
for ch in charts:
series: List[str] = []
for ser in ch.series:
value_num = ser.val.numRef.f\
if hasattr(ser.val, "numRef") and hasattr(ser.val.numRef, "f")\
else ""
value_str = ser.val.strRef.f\
if hasattr(ser.val, "strRef") and hasattr(ser.val.strRef, "f")\
else ""
categ_num = ser.cat.numRef.f\
if hasattr(ser.cat, "numRef") and hasattr(ser.cat.numRef, "f")\
else ""
categ_str = ser.cat.strRef.f\
if hasattr(ser.cat, "strRef") and hasattr(ser.cat.strRef, "f")\
else ""
series.append( "{:},{:},{:},{:}".format( value_num, value_str
, categ_num, categ_str
)
)
series: str = ";".join(series)
# TODO: maybe more aspects, like chart type
info: Dict[str, Any] = {}
chart_set[series] = info
return chart_set
def compare_with_charts(actual: str, expected: str) -> float:
df1 = pd.read_excel(actual)
df2 = pd.read_excel(expected)
normal_content_metric: bool = df1.equals(df2)
print("Normal Contents Metric: {:}".format(normal_content_metric))
charts1 = _load_charts(actual)
charts2 = _load_charts(expected)
chart_metric: bool = charts1==charts2
print("Chart Metric: {:}".format(chart_metric))
return float(normal_content_metric and chart_metric)
def check_sheet_list(result: str, rules: List[Dict[str, Any]]) -> float:
#workbook: Workbook = openpyxl.load_workbook(filename=result)
workbook = pd.ExcelFile(result)
@@ -131,18 +185,42 @@ if __name__ == '__main__':
#rule = {"position": "C6"}
#print(check_xlsx_freeze(path1, rule))
path1 = "../../../../../任务数据/LibreOffice Calc/copy_sheet_insert_gold.xlsx"
rule = [ { "type": "sheet_name"
, "sheet_idx": 0
, "sheet_name": "Sheet1"
}
, { "type": "sheet_data"
, "sheet_idx0": "../../../../../任务数据/LibreOffice Calc/copy_sheet_insert.xlsx@0"
, "sheet_idx1": 1
}
, { "type": "sheet_name"
, "sheet_idx": 2
, "sheet_name": "Sheet2"
}
]
print(check_sheet_list(path1, rule))
#path1 = "../../../../../任务数据/LibreOffice Calc/copy_sheet_insert_gold.xlsx"
#rule = [ { "type": "sheet_name"
#, "sheet_idx": 0
#, "sheet_name": "Sheet1"
#}
#, { "type": "sheet_data"
#, "sheet_idx0": "../../../../../任务数据/LibreOffice Calc/copy_sheet_insert.xlsx@0"
#, "sheet_idx1": 1
#}
#, { "type": "sheet_name"
#, "sheet_idx": 2
#, "sheet_name": "Sheet2"
#}
#]
#print(check_sheet_list(path1, rule))
path1 = "../../../../../任务数据/LibreOffice Calc/Create_column_charts_using_statistics_gold.xlsx"
#workbook1: Workbook = openpyxl.load_workbook(filename=path1)
#worksheet1: Worksheet = workbook1.active
#charts: List[ChartBase] = worksheet1._charts
#print(len(charts))
#print(type(charts[0]))
#
#print(len(charts[0].series))
#print(type(charts[0].series[0]))
#print(type(charts[0].series[0].val))
##print(charts[0].series[0].val)
#print(charts[0].series[0].val.numRef.f)
#
#print(type(charts[0].series[0].cat))
##print(charts[0].series[0].cat)
#print(charts[0].series[0].cat.numRef)
#print(charts[0].series[0].cat.strRef)
#print(charts[0].series[0].cat.strRef.f)
#
#df1 = pd.read_excel(path1)
#print(df1)
path2 = "../../../../../任务数据/LibreOffice Calc/Create_column_charts_using_statistics_gold2.xlsx"
print(compare_with_charts(path1, path2))

View File

@@ -26,6 +26,7 @@
"libreoffice calc"
],
"evaluator": {
"func": "compare_with_sparklines",
"expected": {
"type": "cloud_file",
"path": "https://101.43.24.67/s/t7pgJxNoAGFQWEM/download/OrderId_Month_Chart_gold.xlsx",

View File

@@ -0,0 +1,42 @@
{
"id": "347ef137-7eeb-4c80-a3bb-0951f26a8aff",
"snapshot": "libreoffice_calc",
"instruction": "Could you create two column charts of per-month costs for me using statistics in the form?",
"source": "https://www.youtube.com/watch?v=bgO40-CjYNY",
"config": [
{
"type": "download",
"parameters": {
"files": [
{
"url": "https://101.43.24.67/s/s7aAngonFwaygHr/download/Create_column_charts_using_statistics.xlsx",
"path": "/home/david/Create_column_charts_using_statistics.xlsx"
}
]
}
},
{
"type": "open",
"parameters": {
"path": "/home/david/Create_column_charts_using_statistics.xlsx"
}
}
],
"trajectory": "trajectories/347ef137-7eeb-4c80-a3bb-0951f26a8aff",
"related_apps": [
"libreoffice_calc"
],
"evaluator": {
"func": "compare_with_charts",
"result": {
"type": "vm_file",
"path": "/home/david/Create_column_charts_using_statistics.xlsx",
"dest": "Create_column_charts_using_statistics.xlsx"
},
"expected": {
"type": "cloud_file",
"path": "https://101.43.24.67/s/SLL4CgyMiyre3Ss/download/Create_column_charts_using_statistics_gold.xlsx",
"dest": "Create_column_charts_using_statistics_gold.xlsx"
}
}
}