Skip to content

Commit 1b2971e

Browse files
MacMac
authored andcommitted
544_table_percentage
1 parent 191ad03 commit 1b2971e

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

app/main/check_packs/pack_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
["literature_references"],
3939
["image_references"],
4040
["table_references"],
41+
["table_percentage"],
4142
["first_pages_check"],
4243
["main_character_check"],
4344
["needed_headers_check"],

app/main/checks/report_checks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .literature_references import ReferencesToLiteratureCheck
99
from .image_references import ImageReferences
1010
from .table_references import TableReferences
11+
from .table_percentage import TablePercentage
1112
from .main_text_check import ReportMainTextCheck
1213
from .needed_headers_check import ReportNeededHeadersCheck
1314
from .page_counter import ReportPageCounter
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
2+
import fitz
3+
from docx import Document
4+
from ..base_check import BaseReportCriterion, answer
5+
6+
default_font_size: int = 12 # Значение размера шрифта по умолчанию
7+
default_line_spacing: float = 1.0 # Значение межстрочного интервала по умолчанию
8+
chars_per_line: int = 20 # Примерное кол-во символов в строке ячейки
9+
cell_padding = 5 # Примерный размер отступов от границ ячейки
10+
row_padding = 2 # Примерное расстояние между строками таблиц
11+
12+
#С данными значениями погрешность 4-5 процентов
13+
14+
15+
class TablePercentage(BaseReportCriterion):
16+
label = "Проверка процентного соотношения таблиц в документе"
17+
description = "Проверяет, что таблицы занимают не более установленного процента площади документа"
18+
id = 'table_percentage'
19+
20+
21+
def __init__(self, file_info, max_percentage = 30):
22+
super().__init__(file_info)
23+
self._max_percentage = max_percentage
24+
25+
def get_font_size(self, run, paragraph) -> float:
26+
"""Функция получения размера шрифта"""
27+
if run.font.size:
28+
return run.font.size.pt
29+
elif run.style and run.style.font.size:
30+
return run.style.font.size.pt
31+
elif paragraph.style and paragraph.style.font.size:
32+
return paragraph.style.font.size.pt
33+
else:
34+
return default_font_size
35+
36+
def heightCell(self, cell) -> float:
37+
"""Функция получения высоты ячейки"""
38+
total_height = 0
39+
40+
for paragraph in cell.paragraphs:
41+
line_count = 1
42+
43+
text_length = len(paragraph.text)
44+
if text_length > 0:
45+
line_count = max(1, (text_length + chars_per_line - 1) // chars_per_line)
46+
47+
max_font_size = default_font_size
48+
for run in paragraph.runs:
49+
font_size = self.get_font_size(run, paragraph)
50+
max_font_size = max(max_font_size, font_size)
51+
52+
line_spacing = default_line_spacing
53+
54+
paragraph_height = line_count * max_font_size * line_spacing
55+
total_height += paragraph_height
56+
57+
if paragraph.paragraph_format.space_after:
58+
total_height += paragraph.paragraph_format.space_after.pt
59+
if paragraph.paragraph_format.space_before:
60+
total_height += paragraph.paragraph_format.space_before.pt
61+
62+
total_height += 2 * cell_padding
63+
64+
return total_height
65+
66+
def heightTable(self, table) -> float:
67+
"""Функция получения высоты таблицы"""
68+
total_height = 0
69+
for row in table.rows:
70+
if row.height:
71+
total_height += row.height.pt
72+
else:
73+
heights = []
74+
for cell in row.cells:
75+
heights.append(self.heightCell(cell))
76+
total_height += max(heights) if heights else 0
77+
78+
total_height += row_padding
79+
80+
return total_height
81+
82+
def getPercentOfTables(self) -> float:
83+
"""Функция получение процента таблиц в документе"""
84+
doc_docx = self.file.file
85+
86+
page_count = self.file.page_counter()
87+
if page_count == 0:
88+
return 0
89+
90+
section = doc_docx.sections[0]
91+
page_height = section.page_height.pt
92+
total_height = page_height * page_count
93+
94+
if total_height == 0:
95+
return 0
96+
97+
tables_height = 0
98+
99+
for table in doc_docx.tables:
100+
table_height = self.heightTable(table)
101+
tables_height += table_height
102+
103+
percentage = (tables_height / total_height) * 100
104+
return percentage
105+
106+
def check(self):
107+
try:
108+
if self.file.page_counter() < 4:
109+
return answer(False, "В отчете недостаточно страниц. Нечего проверять.")
110+
111+
percent_tables = self.getPercentOfTables()
112+
113+
if percent_tables <= self._max_percentage:
114+
return answer(
115+
True,
116+
"Пройдена!"
117+
)
118+
else:
119+
return answer(
120+
False,
121+
f"Таблицы занимают {percent_tables:.1f}% документа, "
122+
f"что превышает допустимые {self._max_percentage}%. "
123+
f"Рекомендуется сократить количество или размер таблиц."
124+
)
125+
except Exception as e:
126+
return answer(False, f"Ошибка при проверке процентного соотношения таблиц: {str(e)}")
127+

0 commit comments

Comments
 (0)