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()
随便写了个Python脚本
于 2024-05-18 01:22:09 首次发布