You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
report_app/app/tools/style.py

34 lines
1.2 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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}")