随便写了个Python脚本

from docx import Document
from docx.shared import Pt
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_LINE_SPACING

def set_paragraph_format(paragraph, font_name, font_size, line_spacing, align_center=False, indent_first_line=False, bold=False, line_spacing_rule=WD_LINE_SPACING.MULTIPLE):
    # 设置字体和加粗属性
    for run in paragraph.runs:
        run.font.name = font_name
        run._element.rPr.rFonts.set(qn('w:eastAsia'), font_name)
        run.font.size = Pt(font_size)
        run.font.bold = bold  # 根据需要设置加粗

    # 设置段落格式
    paragraph_format = paragraph.paragraph_format
    paragraph_format.space_before = Pt(0)  # 段前0行
    paragraph_format.space_after = Pt(0)   # 段后0行
    paragraph_format.line_spacing_rule = line_spacing_rule
    paragraph_format.line_spacing = Pt(line_spacing) if line_spacing_rule == WD_LINE_SPACING.EXACTLY else line_spacing  # 固定行间距

    # 如果需要居中对齐
    if align_center:
        paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

    # 如果需要首行缩进
    if indent_first_line:
        paragraph_format.first_line_indent = Pt(22)  # 2字符的缩进,大约为22磅

def format_document(doc):
    for paragraph in doc.paragraphs:
        if paragraph.style.name.startswith('Heading 1'):
            set_paragraph_format(paragraph, font_name='黑体', font_size=16, line_spacing=2.41, align_center=True, bold=True, line_spacing_rule=WD_LINE_SPACING.MULTIPLE)
        elif paragraph.style.name.startswith('Heading 2'):
            set_paragraph_format(paragraph, font_name='黑体', font_size=15, line_spacing=1.73, bold=False, line_spacing_rule=WD_LINE_SPACING.MULTIPLE)
        elif paragraph.style.name.startswith('Heading 3') or paragraph.style.name.startswith('Heading'):
            set_paragraph_format(paragraph, font_name='宋体', font_size=14, line_spacing=1.73, bold=False, line_spacing_rule=WD_LINE_SPACING.MULTIPLE)
        else:
            set_paragraph_format(paragraph, font_name='宋体', font_size=12, line_spacing=22, indent_first_line=True, bold=False, line_spacing_rule=WD_LINE_SPACING.EXACTLY)

    # 设置所有图片的段落为嵌入型并单倍行距
    for inline_shape in doc.inline_shapes:
        # 获取包含图片的段落
        paragraph = inline_shape._inline.getparent().getparent().getparent()
        paragraph_format = paragraph.paragraph_format
        paragraph_format.line_spacing_rule = WD_LINE_SPACING.SINGLE  # 单倍行距
        paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 左对齐

def set_footer_font(footer):
    for paragraph in footer.paragraphs:
        for run in paragraph.runs:
            run.font.name = '等线'
            run._element.rPr.rFonts.set(qn('w:eastAsia'), '等线')
            run.font.size = Pt(9)  # 小五号字体

def main():
    # 加载现有的文档
    doc = Document('formatted_document副本.docx')

    # 设置页面大小为A4
    sections = doc.sections
    for section in sections:
        section.page_width = Pt(595.3)
        section.page_height = Pt(841.9)
        section.left_margin = Pt(2.4 * 28.35)  # 转换为pt单位
        section.right_margin = Pt(2.3 * 28.35)
        section.top_margin = Pt(2.3 * 28.35)
        section.bottom_margin = Pt(2.0 * 28.35)
        section.footer_distance = Pt(1.5 * 28.35)

    # 设置页码格式
    footer = sections[0].footer
    footer_paragraph = footer.paragraphs[0]
    footer_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

    # 清除现有的页码字段
    footer_paragraph.clear()

    # 添加页码域
    page_run = footer_paragraph.add_run()
    fldChar1 = OxmlElement('w:fldChar')  # 创建一个元素
    fldChar1.set(qn('w:fldCharType'), 'begin')  # 设置域字符类型为begin
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')
    instrText.text = 'PAGE'  # 设置域的文本
    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:fldChar')
    fldChar3.set(qn('w:fldCharType'), 'end')

    page_run._r.append(fldChar1)
    page_run._r.append(instrText)
    page_run._r.append(fldChar2)
    page_run._r.append(fldChar3)

    # 设置页码字体为等线,小五号
    set_footer_font(footer)

    # 格式化文档
    format_document(doc)

    # 保存文档,覆盖 existing 'formatted_document副本.docx' if it exists
    output_filename = 'formatted_document副本1.docx'
    doc.save(output_filename)

    print(f"文档已成功保存为 {output_filename}")

if __name__ == '__main__':
    main()
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值