From 757492886eb41b81f6ce1092676b3cd0bbec2a67 Mon Sep 17 00:00:00 2001 From: hexin Date: Thu, 23 Feb 2023 10:23:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GenReport/pdf.py | 456 +++++++++++++++++++++++++----------------- GenReport/pdfmaker.py | 79 ++++---- 2 files changed, 321 insertions(+), 214 deletions(-) diff --git a/GenReport/pdf.py b/GenReport/pdf.py index 658aa7d..fe4a98d 100755 --- a/GenReport/pdf.py +++ b/GenReport/pdf.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 from pdfmaker import PdfMaker -from reportlab.platypus import PageBreak +from reportlab.platypus import PageBreak from reportlab.lib import colors # 颜色模块 from reportlab.platypus import Spacer import json @@ -19,53 +19,62 @@ g_server = "../Outputs/service_result.json" g_result = "./操作系统应用编程接口要求标准符合性测试报告.pdf" g_isConform = "符合" -#table_background_color = '#87CEEB' +# table_background_color = '#87CEEB' default_style = [ - ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 - ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 水平居中 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 垂直居中对齐 - # ('ALIGN', (0, 1), (-1, -1), 'CENTER'), # 第二行到最后一行左右左对齐 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 - ('TEXTCOLOR', (0, 0), (-1, -1), colors.darkslategray), # 设置表格内文字颜色 - ('GRID', (0, 0), (-1, -1), 0.5, colors.black), # 设置表格框线为grey色,线宽为0.5 - ] + ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 + ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 水平居中 + ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 垂直居中对齐 + # ('ALIGN', (0, 1), (-1, -1), 'CENTER'), # 第二行到最后一行左右左对齐 + ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 + ('TEXTCOLOR', (0, 0), (-1, -1), colors.darkslategray), # 设置表格内文字颜色 + ('GRID', (0, 0), (-1, -1), 0.5, colors.black), # 设置表格框线为grey色,线宽为0.5 +] home_table_style = [ - ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 - ('FONTSIZE', (0, 0), (-1, -1), 20), - ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 第一行水平居中 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 - ('ALIGN', (0, 0), (0, -1), 'RIGHT'), - ('ALIGN', (1, 0), (1, -1), 'CENTER'), + ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 + ('FONTSIZE', (0, 0), (-1, -1), 20), + ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 第一行水平居中 + ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 + ('ALIGN', (0, 0), (0, -1), 'RIGHT'), + ('ALIGN', (1, 0), (1, -1), 'CENTER'), ] + def make_pdf_cover(content): # 添加标题 content.append(Spacer(1, 30)) - content.append(PdfMaker.draw_title('《操作系统应用编程接口要求》\n标准符合性测试报告',30)) - + content.append(PdfMaker.draw_title('《操作系统应用编程接口要求》\n标准符合性测试报告', 30)) + # 添加日期等内容 content.append(Spacer(1, 50)) if not os.path.exists(g_env): print("未发现测试环境结果文件") - content.append(PdfMaker.draw_little_title('未发现测试环境结果文件', 12, colors.red)) + content.append( + PdfMaker.draw_little_title( + '未发现测试环境结果文件', 12, colors.red)) return - with open(g_env,'r',encoding='utf8') as fp: + with open(g_env, 'r', encoding='utf8') as fp: json_data = json.load(fp) data = [ ('产品名称:', json_data["测试对象"]["系统名称"]), ('产品版本:', json_data["测试对象"]["版本"]), ('测试日期:', json_data["测试时间"]) ] - content.append(PdfMaker.draw_table(home_table_style, totalWidth=500, rates=[2,7], *data)) + content.append( + PdfMaker.draw_table( + home_table_style, + totalWidth=500, + rates=[2, 7], + *data)) # 添加检测机构 content.append(Spacer(1, 100)) unit = json_data["送测单位"] - content.append(PdfMaker.draw_title(unit,20)) + content.append(PdfMaker.draw_title(unit, 20)) + def make_pdf_test_env(content): # 添加小标题 @@ -73,11 +82,13 @@ def make_pdf_test_env(content): if not os.path.exists(g_env): print("未发现测试环境结果文件") - content.append(PdfMaker.draw_little_title('未发现测试环境结果文件', 12, colors.red)) + content.append( + PdfMaker.draw_little_title( + '未发现测试环境结果文件', 12, colors.red)) return data = [] - with open(g_env,'r',encoding='utf8') as fp: + with open(g_env, 'r', encoding='utf8') as fp: json_data = json.load(fp) data = [ ('测试对象', '系统名称', json_data["测试对象"]["系统名称"]), @@ -96,17 +107,22 @@ def make_pdf_test_env(content): ('测试工具', '版本', json_data["测试工具"]["版本"]), ('测试时间', '测试时间', json_data["测试时间"]), ] - - extra_style = default_style + [ - ('SPAN', (0, 0), (0, 1)), - ('SPAN', (0, 2), (1, 2)), - ('SPAN', (0, 3), (0, 5)), - ('SPAN', (0, 6), (0, 11)), - ('SPAN', (0, 12), (0, 13)), - ] - content.append(PdfMaker.draw_table(extra_style, totalWidth=total_width, rowHeight=27, rates=[1,1,3], *data)) + extra_style = default_style + [ + ('SPAN', (0, 0), (0, 1)), + ('SPAN', (0, 2), (1, 2)), + ('SPAN', (0, 3), (0, 5)), + ('SPAN', (0, 6), (0, 11)), + ('SPAN', (0, 12), (0, 13)), + ] + content.append( + PdfMaker.draw_table( + extra_style, + totalWidth=total_width, + rowHeight=27, + rates=[1, 1, 3], + *data)) def make_pdf_lib(content): @@ -117,7 +133,9 @@ def make_pdf_lib(content): if not os.path.exists(g_lib): print("未发现运行库检查结果文件") - content.append(PdfMaker.draw_little_title('未发现运行库检查结果文件', 12, colors.red)) + content.append( + PdfMaker.draw_little_title( + '未发现运行库检查结果文件', 12, colors.red)) return # (1)库包检查 @@ -128,61 +146,61 @@ def make_pdf_lib(content): ('包名', '检测信息', '--', '--', '--'), ] - with open(g_lib,'r',encoding='utf8') as fp: + with open(g_lib, 'r', encoding='utf8') as fp: json_data = json.load(fp) row = 2 for r in json_data: - + require_version = json_data[r]["Required version"] level = json_data[r]["Level"] data.append((r, '版本要求', '级别', '--', '--')) data.append((r, require_version, level, '--', '--')) data.append((r, '二进制包名', '当前版本', '--', '测试结果')) - + # local_version = "-" # status = "" for pack in json_data[r]["Binary package"]: if isinstance(json_data[r]["Binary package"][pack], dict): - for lib in json_data[r]["Binary package"][pack]: + for lib in json_data[r]["Binary package"][pack]: local_version = json_data[r]["Binary package"][pack][lib]["version"] status = json_data[r]["Binary package"][pack][lib]["status"] - - if status == "compatible" : + + if status == "compatible": status = "通过" elif status == "not installed": continue else: status = "不通过" data.append((r, lib, local_version, '--', status)) - + data.append((r, '共享对象名', '存储路径', '归属包名', '测试结果')) - if isinstance(json_data[r]["Shared library"],dict): + if isinstance(json_data[r]["Shared library"], dict): for shared in json_data[r]["Shared library"]: result = json_data[r]["Shared library"][shared]["status"] - if result == "compatible" : + if result == "compatible": result = "通过" else: result = "不通过" - + belongs = "-" - - if json_data[r]["Shared library"][shared]["belongs"] != "None": + + if json_data[r]["Shared library"][shared]["belongs"] != "None": belongs = json_data[r]["Shared library"][shared]["belongs"] path = json_data[r]["Shared library"][shared]["path"] if len(path) > 65: - path_list =list(path) + path_list = list(path) path_list.insert(60, '\n') path = ''.join(path_list) data.append((r, shared, path, belongs, result)) - + lib_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 1), 15), # 第一,二行的字体大小 - ('FONTSIZE', (0, 2), (-1, -1), 10), # 第三行到最后一行的字体大小 - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 - ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 - ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 - ('SPAN', (1, 0), (-1, 1)), # 合并第四列一二行 - ] + ('FONTSIZE', (0, 0), (-1, 1), 15), # 第一,二行的字体大小 + ('FONTSIZE', (0, 2), (-1, -1), 10), # 第三行到最后一行的字体大小 + ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 + ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 + ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 + ('SPAN', (1, 0), (-1, 1)), # 合并第四列一二行 + ] row = 10 i = 2 @@ -190,45 +208,55 @@ def make_pdf_lib(content): is_merge = False while i < row: if new_data[i][1] == "版本要求": - lib_table_style.append(('BACKGROUND', (1, i), (-1, i), table_background_color)) + lib_table_style.append( + ('BACKGROUND', (1, i), (-1, i), table_background_color)) lib_table_style.append(('SPAN', (2, i), (-1, i))) - lib_table_style.append(('SPAN', (2, i+1), (-1, i+1))) + lib_table_style.append(('SPAN', (2, i + 1), (-1, i + 1))) if new_data[i][1] == "二进制包名": - lib_table_style.append(('BACKGROUND', (1, i), (-1, i), table_background_color)) + lib_table_style.append( + ('BACKGROUND', (1, i), (-1, i), table_background_color)) is_merge = True - if new_data[i][1] == "共享对象名": - lib_table_style.append(('BACKGROUND', (1, i), (-1, i), table_background_color)) + lib_table_style.append( + ('BACKGROUND', (1, i), (-1, i), table_background_color)) is_merge = False - + if is_merge: lib_table_style.append(('SPAN', (2, i), (3, i))) if new_data[i][-1] == "不通过": lib_table_style.append(('TEXTCOLOR', (0, i), (-1, i), colors.red)) - + i += 1 - content.append(PdfMaker.draw_mul_table(lib_table_style, totalWidth=total_width, rates=[2,3,7,2,1], *new_data)) - while row Date: Thu, 23 Feb 2023 10:26:12 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=8C=85=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=A4=B1=E8=B4=A5=E7=9A=84=E7=BB=9F=E8=AE=A1=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GenReport/pdf.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/GenReport/pdf.py b/GenReport/pdf.py index fe4a98d..b37ba8d 100755 --- a/GenReport/pdf.py +++ b/GenReport/pdf.py @@ -297,10 +297,13 @@ def make_pdf_lib(content): for pack in json_data[r]["Binary package"]: if isinstance(json_data[r]["Binary package"][pack], dict): + pack_passed = True for lib in json_data[r]["Binary package"][pack]: status = json_data[r]["Binary package"][pack][lib]["status"] if status == "incompatible": - result_map[level]["pack_no_pass_num"] += 1 + pack_passed = False + if not pack_passed: + result_map[level]["pack_no_pass_num"] += 1 if isinstance(json_data[r]["Shared library"], dict): for lib in json_data[r]["Shared library"]: -- Gitee From f6ba7e4f7791dc63a7326e4043f3564f9da080be Mon Sep 17 00:00:00 2001 From: hexin Date: Thu, 23 Feb 2023 18:22:30 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=A0=BC=E5=86=85=E7=9A=84=E6=8D=A2=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GenReport/pdf.py | 347 ++++++++++++++++++++++++++---------------- GenReport/pdfmaker.py | 36 +++-- 2 files changed, 240 insertions(+), 143 deletions(-) diff --git a/GenReport/pdf.py b/GenReport/pdf.py index b37ba8d..47b08d2 100755 --- a/GenReport/pdf.py +++ b/GenReport/pdf.py @@ -4,6 +4,8 @@ from pdfmaker import PdfMaker from reportlab.platypus import PageBreak from reportlab.lib import colors # 颜色模块 from reportlab.platypus import Spacer +from reportlab.lib.styles import getSampleStyleSheet # 样式 +from reportlab.platypus import Paragraph import json import argparse import os @@ -19,25 +21,34 @@ g_server = "../Outputs/service_result.json" g_result = "./操作系统应用编程接口要求标准符合性测试报告.pdf" g_isConform = "符合" +# 表格样式 # table_background_color = '#87CEEB' -default_style = [ - ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 - ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 水平居中 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 垂直居中对齐 - # ('ALIGN', (0, 1), (-1, -1), 'CENTER'), # 第二行到最后一行左右左对齐 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 - ('TEXTCOLOR', (0, 0), (-1, -1), colors.darkslategray), # 设置表格内文字颜色 - ('GRID', (0, 0), (-1, -1), 0.5, colors.black), # 设置表格框线为grey色,线宽为0.5 +default_table_style = [ + ['ALIGN', (0, 0), (-1, -1), 'CENTER'], # 水平居中 + ['VALIGN', (0, 0), (-1, -1), 'MIDDLE'], # 垂直居中对齐 + # ['ALIGN', (0, 1), (-1, -1), 'CENTER'], # 第二行到最后一行左右左对齐 + ['VALIGN', (0, 0), (-1, -1), 'MIDDLE'], # 所有表格上下居中对齐 + ['GRID', (0, 0), (-1, -1), 0.5, colors.black], # 设置表格框线为grey色,线宽为0.5 ] - +default_para_style = getSampleStyleSheet()['Normal'] +default_para_style.fontName = 'SimSun' +default_para_style.wordWrap = 'CJK' # 设置自动换行 +default_para_style.fontSize = 10 +default_para_style.textColor = colors.darkslategray # 设置表格内文字颜色 +default_para_style.alignment = 1 # 居中对齐 + +# 首页样式 home_table_style = [ - ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 - ('FONTSIZE', (0, 0), (-1, -1), 20), - ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 第一行水平居中 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 - ('ALIGN', (0, 0), (0, -1), 'RIGHT'), - ('ALIGN', (1, 0), (1, -1), 'CENTER'), + ['ALIGN', (0, 0), (-1, -1), 'CENTER'], # 第一行水平居中 + ['VALIGN', (0, 0), (-1, -1), 'MIDDLE'], # 所有表格上下居中对齐 + ['ALIGN', (0, 0), (0, -1), 'RIGHT'], + ['ALIGN', (1, 0), (1, -1), 'CENTER'], ] +home_para_style = getSampleStyleSheet()['Normal'] +home_para_style.fontName = 'SimSun' +home_para_style.fontSize = 20 +home_para_style.wordWrap = 'CJK' # 设置自动换行 +home_para_style.alignment = 1 # 居中对齐 def make_pdf_cover(content): @@ -58,16 +69,21 @@ def make_pdf_cover(content): with open(g_env, 'r', encoding='utf8') as fp: json_data = json.load(fp) data = [ - ('产品名称:', json_data["测试对象"]["系统名称"]), - ('产品版本:', json_data["测试对象"]["版本"]), - ('测试日期:', json_data["测试时间"]) + ['产品名称:', json_data["测试对象"]["系统名称"]], + ['产品版本:', json_data["测试对象"]["版本"]], + ['测试日期:', json_data["测试时间"]] ] + + for i, row in enumerate(data): + for j, cell in enumerate(row): + data[i][j] = Paragraph(str(cell), home_para_style) + content.append( PdfMaker.draw_table( home_table_style, - totalWidth=500, - rates=[2, 7], - *data)) + data, + totalWidth=550, + rates=[2, 7],)) # 添加检测机构 content.append(Spacer(1, 100)) @@ -91,38 +107,42 @@ def make_pdf_test_env(content): with open(g_env, 'r', encoding='utf8') as fp: json_data = json.load(fp) data = [ - ('测试对象', '系统名称', json_data["测试对象"]["系统名称"]), - ('测试对象', '版本', json_data["测试对象"]["版本"]), - ('送测单位', '送测单位', json_data["送测单位"]), - ('系统环境', '内核版本', json_data["系统环境"]["内核版本"]), - ('系统环境', '编译器版本', json_data["系统环境"]["编译器版本"]), - ('系统环境', 'Python版本', json_data["系统环境"]["Python版本"]), - ('环境配置', '机器型号', json_data["环境配置"]["CPU型号"]), - ('环境配置', 'CPU指令集', json_data["环境配置"]["CPU指令集"]), - ('环境配置', 'CPU型号', json_data["环境配置"]["CPU型号"]), - ('环境配置', '内存', json_data["环境配置"]["内存"]), - ('环境配置', '硬盘', json_data["环境配置"]["硬盘"]), - ('环境配置', '固件', json_data["环境配置"]["固件"]), - ('测试工具', '名称', json_data["测试工具"]["名称"]), - ('测试工具', '版本', json_data["测试工具"]["版本"]), - ('测试时间', '测试时间', json_data["测试时间"]), + ['测试对象', '系统名称', json_data["测试对象"]["系统名称"]], + ['测试对象', '版本', json_data["测试对象"]["版本"]], + ['送测单位', '送测单位', json_data["送测单位"]], + ['系统环境', '内核版本', json_data["系统环境"]["内核版本"]], + ['系统环境', '编译器版本', json_data["系统环境"]["编译器版本"]], + ['系统环境', 'Python版本', json_data["系统环境"]["Python版本"]], + ['环境配置', '机器型号', json_data["环境配置"]["CPU型号"]], + ['环境配置', 'CPU指令集', json_data["环境配置"]["CPU指令集"]], + ['环境配置', 'CPU型号', json_data["环境配置"]["CPU型号"]], + ['环境配置', '内存', json_data["环境配置"]["内存"]], + ['环境配置', '硬盘', json_data["环境配置"]["硬盘"]], + ['环境配置', '固件', json_data["环境配置"]["固件"]], + ['测试工具', '名称', json_data["测试工具"]["名称"]], + ['测试工具', '版本', json_data["测试工具"]["版本"]], + ['测试时间', '测试时间', json_data["测试时间"]], ] - extra_style = default_style + [ - ('SPAN', (0, 0), (0, 1)), - ('SPAN', (0, 2), (1, 2)), - ('SPAN', (0, 3), (0, 5)), - ('SPAN', (0, 6), (0, 11)), - ('SPAN', (0, 12), (0, 13)), + for i, row in enumerate(data): + for j, cell in enumerate(row): + data[i][j] = Paragraph(str(cell), default_para_style) + + extra_style = default_table_style + [ + ['SPAN', (0, 0), (0, 1)], + ['SPAN', (0, 2), (1, 2)], + ['SPAN', (0, 3), (0, 5)], + ['SPAN', (0, 6), (0, 11)], + ['SPAN', (0, 12), (0, 13)], ] content.append( PdfMaker.draw_table( extra_style, + data, totalWidth=total_width, rowHeight=27, - rates=[1, 1, 3], - *data)) + rates=[1, 1, 3])) def make_pdf_lib(content): @@ -142,8 +162,8 @@ def make_pdf_lib(content): content.append(PdfMaker.draw_little_title('(1)库包检查', 12)) # 添加表格 data = [ - ('包名', '检测信息', '--', '--', '--'), - ('包名', '检测信息', '--', '--', '--'), + ['包名', '检测信息', '--', '--', '--'], + ['包名', '检测信息', '--', '--', '--'], ] with open(g_lib, 'r', encoding='utf8') as fp: @@ -153,9 +173,9 @@ def make_pdf_lib(content): require_version = json_data[r]["Required version"] level = json_data[r]["Level"] - data.append((r, '版本要求', '级别', '--', '--')) - data.append((r, require_version, level, '--', '--')) - data.append((r, '二进制包名', '当前版本', '--', '测试结果')) + data.append([r, '版本要求', '级别', '--', '--']) + data.append([r, require_version, level, '--', '--']) + data.append([r, '二进制包名', '当前版本', '--', '测试结果']) # local_version = "-" # status = "" @@ -171,9 +191,9 @@ def make_pdf_lib(content): continue else: status = "不通过" - data.append((r, lib, local_version, '--', status)) + data.append([r, lib, local_version, '--', status]) - data.append((r, '共享对象名', '存储路径', '归属包名', '测试结果')) + data.append([r, '共享对象名', '存储路径', '归属包名', '测试结果']) if isinstance(json_data[r]["Shared library"], dict): for shared in json_data[r]["Shared library"]: result = json_data[r]["Shared library"][shared]["status"] @@ -191,86 +211,105 @@ def make_pdf_lib(content): path_list = list(path) path_list.insert(60, '\n') path = ''.join(path_list) - data.append((r, shared, path, belongs, result)) + data.append([r, shared, path, belongs, result]) - lib_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 1), 15), # 第一,二行的字体大小 - ('FONTSIZE', (0, 2), (-1, -1), 10), # 第三行到最后一行的字体大小 - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 - ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 - ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 - ('SPAN', (1, 0), (-1, 1)), # 合并第四列一二行 + lib_table_style = default_table_style + [ + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 + ['BACKGROUND', (0, 1), (-1, 1), table_background_color], # 设置第二行背景颜色 + ['SPAN', (0, 0), (0, 1)], # 合并第一列一二行 + ['SPAN', (1, 0), (-1, 1)], # 合并第四列一二行 ] row = 10 - i = 2 new_data = data[0:row] is_merge = False - while i < row: + + for i, r in enumerate(new_data): + if i >= row: + break if new_data[i][1] == "版本要求": lib_table_style.append( - ('BACKGROUND', (1, i), (-1, i), table_background_color)) - lib_table_style.append(('SPAN', (2, i), (-1, i))) - lib_table_style.append(('SPAN', (2, i + 1), (-1, i + 1))) + ['BACKGROUND', (1, i), (-1, i), table_background_color]) + lib_table_style.append(['SPAN', (2, i), (-1, i)]) + lib_table_style.append(['SPAN', (2, i + 1), (-1, i + 1)]) if new_data[i][1] == "二进制包名": lib_table_style.append( - ('BACKGROUND', (1, i), (-1, i), table_background_color)) + ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = True if new_data[i][1] == "共享对象名": lib_table_style.append( - ('BACKGROUND', (1, i), (-1, i), table_background_color)) - + ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = False if is_merge: - lib_table_style.append(('SPAN', (2, i), (3, i))) + lib_table_style.append(['SPAN', (2, i), (3, i)]) if new_data[i][-1] == "不通过": - lib_table_style.append(('TEXTCOLOR', (0, i), (-1, i), colors.red)) - - i += 1 + c = default_para_style.textColor + default_para_style.textColor = colors.red + new_data[i][-1] = Paragraph(str(new_data[i][-1]), + default_para_style) + default_para_style.textColor = c + + for j, cell in enumerate(r): + if not isinstance(new_data[i][j], Paragraph): + s = default_para_style.fontSize + if i == 0: + default_para_style.fontSize = 12 + new_data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s content.append( PdfMaker.draw_mul_table( lib_table_style, + new_data, totalWidth=total_width, - rates=[2, 3, 7, 2, 1], - *new_data)) + rates=[2, 3, 7, 2, 1.2])) + while row < len(data): i = 0 new_data = data[row:row + 15] - other_style = [('FONTSIZE', (0, 0), (-1, -1), 10), ] - other_style = other_style + default_style - while i < len(new_data): + other_style = [] + default_table_style + for i, r in enumerate(new_data): + if len(new_data) <= i: + break if new_data[i][1] == "版本要求": other_style.append( - ('BACKGROUND', (1, i), (-1, i), table_background_color)) - other_style.append(('SPAN', (2, i), (-1, i))) - other_style.append(('SPAN', (2, i + 1), (-1, i + 1))) + ['BACKGROUND', (1, i), (-1, i), table_background_color]) + other_style.append(['SPAN', (2, i), (-1, i)]) + other_style.append(['SPAN', (2, i + 1), (-1, i + 1)]) if new_data[i][1] == "二进制包名": other_style.append( - ('BACKGROUND', (1, i), (-1, i), table_background_color)) + ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = True if new_data[i][1] == "共享对象名": other_style.append( - ('BACKGROUND', (1, i), (-1, i), table_background_color)) + ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = False if is_merge: - other_style.append(('SPAN', (2, i), (3, i))) + other_style.append(['SPAN', (2, i), (3, i)]) if new_data[i][-1] == "不通过": - other_style.append(('TEXTCOLOR', (1, i), (-1, i), colors.red)) - i += 1 + c = default_para_style.textColor + default_para_style.textColor = colors.red + new_data[i][-1] = Paragraph(str(new_data[i][-1]), + default_para_style) + default_para_style.textColor = c + + for j, cell in enumerate(r): + if not isinstance(new_data[i][j], Paragraph): + new_data[i][j] = Paragraph(str(cell), default_para_style) + content.append( PdfMaker.draw_mul_table( other_style, + new_data, totalWidth=total_width, - rates=[2, 3, 7, 2, 1], - *new_data)) + rates=[2, 3, 7, 2, 1.2])) row += 15 content.append(PageBreak()) @@ -313,13 +352,11 @@ def make_pdf_lib(content): # 添加表格 data = [ - ('级别', '类型', '要求数量', '符合数量', '比率'), + ['级别', '类型', '要求数量', '符合数量', '比率'], ] i = 1 - result_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 0), 12), # 第一,二行的字体大小 - ('FONTSIZE', (0, 2), (-1, -1), 10), # 第三行到最后一行的字体大小 - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 + result_style = default_table_style + [ + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 ] for level in result_map: pack_pass_num = result_map[level]["pack_require_num"] - \ @@ -340,20 +377,20 @@ def make_pdf_lib(content): else: shard_rate = 0 - table_content = ( + table_content = [ level, "包", result_map[level]["pack_require_num"], pack_pass_num, - pack_rate) + pack_rate] data.append(table_content) - table_content = ( + table_content = [ level, "共享对象", result_map[level]["shard_require_num"], result_map[level]["shard_pass_num"], - shard_rate) + shard_rate] data.append(table_content) result_style = result_style + [ @@ -367,12 +404,22 @@ def make_pdf_lib(content): g_isConform = "不符合" i += 2 + + for i, row in enumerate(data): + for j, cell in enumerate(row): + s = default_para_style.fontSize + if i == 0: + default_para_style.fontSize = 12 + data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s + content.append( PdfMaker.draw_table( result_style, + data, totalWidth=total_width, - rates=[1, 1, 1, 1, 1], - *data)) + rates=[1, 1, 1, 1, 1] + )) def make_pdf_fs(content): @@ -400,9 +447,7 @@ def make_pdf_fs(content): require_num = 0 pass_num = 0 - fs_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 0), 12), # 第一行的字体大小 - ('FONTSIZE', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小 + fs_table_style = default_table_style + [ ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 @@ -426,8 +471,6 @@ def make_pdf_fs(content): pass_num += 1 else: result = "不符合" - fs_table_style.append( - ('TEXTCOLOR', (0, i), (-1, i), colors.red)) table_content = ( r["FS_name"], @@ -439,12 +482,24 @@ def make_pdf_fs(content): i += 1 require_num += 1 + for i, row in enumerate(data): + for j, cell in enumerate(row): + s = default_para_style.fontSize + c = default_para_style.textColor + if i == 0: + default_para_style.fontSize = 12 + if j == len(row) - 1 and cell == "不符合": + default_para_style.textColor = colors.red + data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s + default_para_style.textColor = c + content.append( PdfMaker.draw_table( fs_table_style, + data, totalWidth=total_width, - rates=[1, 2, 2, 1], - *data)) + rates=[1, 2, 2, 1])) content.append(Spacer(1, 10)) # (2)小计 @@ -455,17 +510,23 @@ def make_pdf_fs(content): ('要求数量', '符合数量', '比率'), (require_num, pass_num, rate) ] - result_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 0), 12), # 第一行的字体大小 - ('FONTSIZE', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小 + result_table_style = default_table_style + [ ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 ] + for i, row in enumerate(data): + for j, cell in enumerate(row): + s = default_para_style.fontSize + if i == 0: + default_para_style.fontSize = 12 + data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s + content.append( PdfMaker.draw_table( result_table_style, + data, totalWidth=total_width, - rates=[1, 1, 1], - *data)) + rates=[1, 1, 1])) def make_pdf_cmd(content): @@ -492,9 +553,7 @@ def make_pdf_cmd(content): require_num = 0 pass_num = 0 - cmd_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 0), 12), # 第一行的字体大小 - ('FONTSIZE', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小 + cmd_table_style = default_table_style + [ ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 @@ -521,8 +580,6 @@ def make_pdf_cmd(content): pass_num += 1 else: result = "不符合" - cmd_table_style.append( - ('TEXTCOLOR', (0, i), (-1, i), colors.red)) table_content = (r["name"], "-", r["cmd_version"], check, result) data.append(table_content) @@ -530,12 +587,24 @@ def make_pdf_cmd(content): i += 1 require_num += 1 + for i, row in enumerate(data): + for j, cell in enumerate(row): + s = default_para_style.fontSize + c = default_para_style.textColor + if i == 0: + default_para_style.fontSize = 12 + if j == len(row) - 1 and cell == "不符合": + default_para_style.textColor = colors.red + data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s + default_para_style.textColor = c + content.append( PdfMaker.draw_table( cmd_table_style, + data, totalWidth=total_width, - rates=[1, 4, 2, 2, 2], - *data)) + rates=[1, 4, 2, 2, 2])) # (2)小计 content.append(PdfMaker.draw_little_title('(2)小计', 12)) @@ -545,17 +614,25 @@ def make_pdf_cmd(content): ('要求数量', '符合数量', '比率'), (require_num, pass_num, rate) ] - result_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 0), 12), # 第一行的字体大小 - ('FONTSIZE', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小 + result_table_style = default_table_style + [ ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 ] + for i, row in enumerate(data): + for j, cell in enumerate(row): + s = default_para_style.fontSize + if i == 0: + default_para_style.fontSize = 12 + if j == len(row) - 1 and cell == "不符合": + default_para_style.textColor = colors.red + data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s + content.append( PdfMaker.draw_table( result_table_style, + data, totalWidth=total_width, - rates=[1, 1, 1], - *data)) + rates=[1, 1, 1])) def make_pdf_service(content): @@ -575,13 +652,11 @@ def make_pdf_service(content): content.append(PdfMaker.draw_little_title('(1)常用服务检查', 12)) # 添加表格 data = [ - ('服务', '测试结果'), + ['服务', '测试结果'], ] - service_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 0), 12), # 第一行的字体大小 - ('FONTSIZE', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小 - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 + service_style = default_table_style + [ + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 ] with open(g_server, 'r', encoding='utf8') as fp: @@ -594,17 +669,29 @@ def make_pdf_service(content): else: result = "不通过" service_style.append( - ('TEXTCOLOR', (0, i), (-1, i), colors.red)) - table_content = (r, result) + ['TEXTCOLOR', (0, i), (-1, i), colors.red]) + table_content = [r, result] data.append(table_content) i += 1 + for i, row in enumerate(data): + for j, cell in enumerate(row): + s = default_para_style.fontSize + c = default_para_style.textColor + if i == 0: + default_para_style.fontSize = 12 + if j == len(row) - 1 and cell == "不符合": + default_para_style.textColor = colors.red + data[i][j] = Paragraph(str(cell), default_para_style) + default_para_style.fontSize = s + default_para_style.textColor = c + content.append( PdfMaker.draw_table( service_style, + data, totalWidth=total_width, - rates=[1, 1], - *data)) + rates=[1, 1])) def make_pdf_result(content): diff --git a/GenReport/pdfmaker.py b/GenReport/pdfmaker.py index 9d3f523..ee9acb5 100644 --- a/GenReport/pdfmaker.py +++ b/GenReport/pdfmaker.py @@ -1,8 +1,9 @@ #!/usr/bin/python3 +import os from reportlab.pdfbase import pdfmetrics # 注册字体 from reportlab.pdfbase.ttfonts import TTFont # 字体类 -from reportlab.platypus import Table, SimpleDocTemplate, Paragraph, Image # 报告内容相关类 +from reportlab.platypus import Table, LongTable, SimpleDocTemplate, Paragraph, Image # 报告内容相关类 # 页面的标志尺寸(8.5*inch, 11*inch) from reportlab.lib.pagesizes import letter, landscape from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle # 文本样式 @@ -14,7 +15,12 @@ from reportlab.graphics.shapes import Drawing # 绘图工具 from reportlab.lib.units import cm # 单位:cm # 注册字体(提前准备好字体文件, 如果同一个文件需要多种字体可以注册多个) -pdfmetrics.registerFont(TTFont('SimSun', "./NotoSansSCRegular.ttf")) +fonts = [ + ["SimSun", "./NotoSansSCRegular.ttf"], +] +for f in fonts: + font_path = os.path.join(os.path.dirname(__file__), f[1]) + pdfmetrics.registerFont(TTFont(f[0], font_path)) class PdfMaker: @@ -78,7 +84,7 @@ class PdfMaker: # 绘制表格 @staticmethod - def draw_table(style, *args, totalWidth, rowHeight=30, rates): + def draw_table(style, tab_dict, totalWidth, rates, rowHeight=30): total_rate = 0 for rate in rates: total_rate += rate @@ -86,9 +92,8 @@ class PdfMaker: col_rate = [] for rate in rates: col_rate.append((rate / total_rate) * totalWidth) - - table = Table( - args, + table = LongTable( + tab_dict, colWidths=col_rate, style=style, rowHeights=rowHeight) @@ -96,7 +101,7 @@ class PdfMaker: # 跨页合并需求 @staticmethod - def draw_mul_table(style, *args, totalWidth, rates): + def draw_mul_table(style, tab_dict, totalWidth, rates): total_rate = 0 for rate in rates: total_rate += rate @@ -108,10 +113,11 @@ class PdfMaker: i = 0 begin = 0 end = 0 - while i < len(args) - 1: - if args[i + 1][0] == args[i][0]: - if i + 1 == len(args) - 1: - end = len(args) - 1 + while i < len(tab_dict) - 1: + if tab_dict[i + + 1][0].getPlainText() == tab_dict[i][0].getPlainText(): + if i + 1 == len(tab_dict) - 1: + end = len(tab_dict) - 1 style = style + [ ('SPAN', (0, begin), (0, end)) ] @@ -124,8 +130,11 @@ class PdfMaker: begin = end + 1 i += 1 - - table = Table(args, colWidths=col_rate, style=style, rowHeights=30) + table = Table( + tab_dict, + colWidths=col_rate, + style=style, + rowHeights=30) # i += 15 return table @@ -170,6 +179,7 @@ class PdfMaker: img.drawHeight = 8 * cm # 设置图片的高度 return img + @staticmethod def create_pdf(filename, content): # 生成pdf文件 doc = SimpleDocTemplate(filename, pagesize=landscape(letter)) -- Gitee From 54c60c3e0cb7ee1ea87c095a8c07115dd7899a9c Mon Sep 17 00:00:00 2001 From: hexin Date: Fri, 24 Feb 2023 18:10:10 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=B7=A8=E9=A1=B5?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=90=88=E5=B9=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GenReport/README.md | 3 +- GenReport/pdf.py | 116 ++++++++++++------------------------------ GenReport/pdfmaker.py | 16 ++++-- 3 files changed, 46 insertions(+), 89 deletions(-) diff --git a/GenReport/README.md b/GenReport/README.md index f4ae805..906c9de 100644 --- a/GenReport/README.md +++ b/GenReport/README.md @@ -5,7 +5,7 @@ json结果转pdf工具 #### 依赖安装 -1. pip3 install reportlab +1. pip3 install reportlab==3.6.12 #### 使用说明 1. 基本用法,直接执行使用默认路径生成pdf @@ -16,6 +16,7 @@ json结果转pdf工具 2. 使用参数指定文件路径 - -r : 指定pdf生成结果路径 + - -e : 指定环境检测结果路径 - -l : 指定运行库检查结果文件路径 - -f : 指定文件系统层次结构检查结果文件路径 - -c : 指定常用命令检查结果文件路径 diff --git a/GenReport/pdf.py b/GenReport/pdf.py index 47b08d2..5cecf48 100755 --- a/GenReport/pdf.py +++ b/GenReport/pdf.py @@ -220,24 +220,20 @@ def make_pdf_lib(content): ['SPAN', (1, 0), (-1, 1)], # 合并第四列一二行 ] - row = 10 - new_data = data[0:row] is_merge = False - for i, r in enumerate(new_data): - if i >= row: - break - if new_data[i][1] == "版本要求": + for i, r in enumerate(data): + if data[i][1] == "版本要求": lib_table_style.append( ['BACKGROUND', (1, i), (-1, i), table_background_color]) lib_table_style.append(['SPAN', (2, i), (-1, i)]) lib_table_style.append(['SPAN', (2, i + 1), (-1, i + 1)]) - if new_data[i][1] == "二进制包名": + if data[i][1] == "二进制包名": lib_table_style.append( ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = True - if new_data[i][1] == "共享对象名": + if data[i][1] == "共享对象名": lib_table_style.append( ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = False @@ -245,73 +241,27 @@ def make_pdf_lib(content): if is_merge: lib_table_style.append(['SPAN', (2, i), (3, i)]) - if new_data[i][-1] == "不通过": + if data[i][-1] == "不通过": c = default_para_style.textColor default_para_style.textColor = colors.red - new_data[i][-1] = Paragraph(str(new_data[i][-1]), - default_para_style) + data[i][-1] = Paragraph(str(data[i][-1]), + default_para_style) default_para_style.textColor = c for j, cell in enumerate(r): - if not isinstance(new_data[i][j], Paragraph): + if not isinstance(data[i][j], Paragraph): s = default_para_style.fontSize if i == 0: default_para_style.fontSize = 12 - new_data[i][j] = Paragraph(str(cell), default_para_style) + data[i][j] = Paragraph(str(cell), default_para_style) default_para_style.fontSize = s content.append( PdfMaker.draw_mul_table( lib_table_style, - new_data, + data, totalWidth=total_width, rates=[2, 3, 7, 2, 1.2])) - - while row < len(data): - i = 0 - new_data = data[row:row + 15] - other_style = [] + default_table_style - for i, r in enumerate(new_data): - if len(new_data) <= i: - break - if new_data[i][1] == "版本要求": - other_style.append( - ['BACKGROUND', (1, i), (-1, i), table_background_color]) - other_style.append(['SPAN', (2, i), (-1, i)]) - other_style.append(['SPAN', (2, i + 1), (-1, i + 1)]) - - if new_data[i][1] == "二进制包名": - other_style.append( - ['BACKGROUND', (1, i), (-1, i), table_background_color]) - is_merge = True - - if new_data[i][1] == "共享对象名": - other_style.append( - ['BACKGROUND', (1, i), (-1, i), table_background_color]) - is_merge = False - - if is_merge: - other_style.append(['SPAN', (2, i), (3, i)]) - - if new_data[i][-1] == "不通过": - c = default_para_style.textColor - default_para_style.textColor = colors.red - new_data[i][-1] = Paragraph(str(new_data[i][-1]), - default_para_style) - default_para_style.textColor = c - - for j, cell in enumerate(r): - if not isinstance(new_data[i][j], Paragraph): - new_data[i][j] = Paragraph(str(cell), default_para_style) - - content.append( - PdfMaker.draw_mul_table( - other_style, - new_data, - totalWidth=total_width, - rates=[2, 3, 7, 2, 1.2])) - row += 15 - content.append(PageBreak()) # (2)小计 @@ -440,19 +390,19 @@ def make_pdf_fs(content): content.append(PdfMaker.draw_little_title('(1)文件系统层次结构检查', 12)) # 添加表格 data = [ - ('目录', '检查项', '', '测试结果'), - ('', '存在', '权限', ''), + ['目录', '检查项', '', '测试结果'], + ['', '存在', '权限', ''], ] require_num = 0 pass_num = 0 fs_table_style = default_table_style + [ - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 - ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 - ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 - ('SPAN', (1, 0), (2, 0)), # 合并第一行二三列 - ('SPAN', (3, 0), (3, 1)), # 合并第四列一二行 + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 + ['BACKGROUND', (0, 1), (-1, 1), table_background_color], # 设置第二行背景颜色 + ['SPAN', (0, 0), (0, 1)], # 合并第一列一二行 + ['SPAN', (1, 0), (2, 0)], # 合并第一行二三列 + ['SPAN', (3, 0), (3, 1)], # 合并第四列一二行 ] with open(g_fs, 'r', encoding='utf8') as fp: @@ -472,11 +422,11 @@ def make_pdf_fs(content): else: result = "不符合" - table_content = ( + table_content = [ r["FS_name"], exist, r["file_permissions"], - result) + result] data.append(table_content) i += 1 @@ -507,11 +457,11 @@ def make_pdf_fs(content): # 添加表格 rate = '{:.2f}%'.format(pass_num / require_num * 100) data = [ - ('要求数量', '符合数量', '比率'), - (require_num, pass_num, rate) + ['要求数量', '符合数量', '比率'], + [require_num, pass_num, rate] ] result_table_style = default_table_style + [ - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 ] for i, row in enumerate(data): for j, cell in enumerate(row): @@ -546,19 +496,19 @@ def make_pdf_cmd(content): content.append(PdfMaker.draw_little_title('(1)常用命令检查', 12)) # 添加表格 data = [ - ('命令', '检查项', '', '', '测试结果'), - ('', '路径', '版本', '运行', ''), + ['命令', '检查项', '', '', '测试结果'], + ['', '路径', '版本', '运行', ''], ] require_num = 0 pass_num = 0 cmd_table_style = default_table_style + [ - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 - ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 - ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 - ('SPAN', (1, 0), (3, 0)), # 合并第一行二三四列 - ('SPAN', (4, 0), (4, 1)), # 合并第五列一二行 + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 + ['BACKGROUND', (0, 1), (-1, 1), table_background_color], # 设置第二行背景颜色 + ['SPAN', (0, 0), (0, 1)], # 合并第一列一二行 + ['SPAN', (1, 0), (3, 0)], # 合并第一行二三四列 + ['SPAN', (4, 0), (4, 1)], # 合并第五列一二行 ] with open(g_cmd, 'r', encoding='utf8') as fp: @@ -581,7 +531,7 @@ def make_pdf_cmd(content): else: result = "不符合" - table_content = (r["name"], "-", r["cmd_version"], check, result) + table_content = [r["name"], "-", r["cmd_version"], check, result] data.append(table_content) i += 1 @@ -611,11 +561,11 @@ def make_pdf_cmd(content): # 添加表格 rate = '{:.2f}%'.format(pass_num / require_num * 100) data = [ - ('要求数量', '符合数量', '比率'), - (require_num, pass_num, rate) + ['要求数量', '符合数量', '比率'], + [require_num, pass_num, rate] ] result_table_style = default_table_style + [ - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 ] for i, row in enumerate(data): for j, cell in enumerate(row): diff --git a/GenReport/pdfmaker.py b/GenReport/pdfmaker.py index ee9acb5..74ecde6 100644 --- a/GenReport/pdfmaker.py +++ b/GenReport/pdfmaker.py @@ -23,6 +23,13 @@ for f in fonts: pdfmetrics.registerFont(TTFont(f[0], font_path)) +class Custom_tab(Table): + # hack的table类,用于多页的单元格合并,当前仅在reportlab=3.6.12下测试 + @staticmethod + def _getRowImpossible(impossible, cells, ranges): + impossible = {} + + class PdfMaker: # 绘制标题 content_style = ParagraphStyle( @@ -128,14 +135,13 @@ class PdfMaker: ] begin = end + 1 - i += 1 - table = Table( + + table = Custom_tab( tab_dict, colWidths=col_rate, - style=style, - rowHeights=30) - # i += 15 + style=style) + return table # 创建图表 -- Gitee