from docx.shared import Pt
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH

import logging

# 获取日志记录器
logger = logging.getLogger(__name__)


def table_style(table):
    try:
        logger.info("开始日报格式渲染")
        # 遍历表格的每一行
        for row in table.rows:
            # 遍历每一行的每个单元格
            for cell in row.cells:
                # 遍历单元格的每个段落
                for paragraph in cell.paragraphs:
                    paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
                    # 遍历段落的每个运行（Run）
                    for run in paragraph.runs:
                        # 设置英文字体（适用于数字和英文）
                        run.font.name = "Times New Roman"
                        #run.font.name = "仿宋"
                        # 设置中文字体
                        run._element.rPr.rFonts.set(qn("w:eastAsia"), "仿宋")
                        # 设置字体大小
                        run.font.size = Pt(11)

    except Exception as e:
        logger.exception(f"文件格式渲染失败：{e}")
        print(f"Error: {e}")
