ГОСТ
This commit is contained in:
Mikan
2025-12-09 13:43:18 +03:00
parent 4a48e8de85
commit d5295b7299
6 changed files with 295 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
import yaml
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.ns import qn
from docx.shared import Pt, Cm, RGBColor
from docx.enum.text import WD_LINE_SPACING, WD_ALIGN_PARAGRAPH
@@ -13,11 +14,12 @@ class StyleRegistry:
"""Создаёт недостающие стили в документе, если их нет."""
for name, cfg in self.config["styles"].items():
try:
doc.styles[name]
style = doc.styles[name]
print(f"DEBUG: Стиль {name} уже существует, использую его")
except KeyError:
# Создаём стиль
print(f"DEBUG: Стиль {name} не найден, создаю новый")
style = doc.styles.add_style(name, WD_STYLE_TYPE.PARAGRAPH)
self.apply_style(style, name)
self.apply_style(style, name)
def apply_page_layout(self, doc: Document):
"""Применяет настройки страницы (поля)."""
@@ -38,6 +40,7 @@ class StyleRegistry:
section.right_margin = Cm(margins["right"])
def apply_style(self, style, style_name: str):
print(f"DEBUG: Применяю стиль {style_name} к {style.name}")
cfg = self.config["styles"].get(style_name)
if not cfg:
return
@@ -47,11 +50,21 @@ class StyleRegistry:
if font_cfg:
font = style.font
font.name = font_cfg.get("name", font.name)
if "name" in font_cfg:
print(f"DEBUG: Устанавливаю font.name = {font_cfg['name']}")
font.name = font_cfg["name"]
font.element.rPr.rFonts.attrib.pop(qn("w:hAnsiTheme"), None)
font.element.rPr.rFonts.attrib.pop(qn("w:AnsiTheme"), None)
font.element.rPr.rFonts.set(qn('w:ascii'), font.name)
font.element.rPr.rFonts.set(qn('w:hAnsi'), font.name)
if "size" in font_cfg:
font.size = Pt(font_cfg["size"])
font.bold = font_cfg.get("bold", font.bold)
font.italic = font_cfg.get("italic", font.italic)
if "bold" in font_cfg:
font.bold = font_cfg["bold"]
if "italic" in font_cfg:
font.italic = font_cfg["italic"]
if "color" in font_cfg:
font.color.rgb = RGBColor.from_string(font_cfg["color"])
if para_cfg:
pf = style.paragraph_format
@@ -70,6 +83,9 @@ class StyleRegistry:
if "first_line_indent" in para_cfg:
from docx.shared import Mm
pf.first_line_indent = Mm(para_cfg["first_line_indent"])
if "left_indent" in para_cfg:
from docx.shared import Cm
pf.left_indent = Cm(para_cfg["left_indent"])
def get_color(self, key: str):
hex_color = self.config.get("colors", {}).get(key, "000000")