首先定义一个类格式,每个不同的id代表了不同的引用样式。
# fdocx 类定义
class fdocx:
def __init__(self):
# 定义不同内容对应的字体和段落格式
self.formats = [
{'id': 1, 'font': '黑体', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},
{'id': 2, 'font': '楷体_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},
{'id': 3, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},
{'id': 4, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
{'id': 5, 'font': '方正小标宋_GBK', 'pt': 22, 'alignment': WD_PARAGRAPH_ALIGNMENT.CENTER, 'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
{'id': 6, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'}, #左对齐963930 :这个号码指的是对齐的样式,是空格几个
{'id': 7, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.RIGHT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'}, #右对齐
{'id': 8, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.CENTER,'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
{'id': 9, 'font': 'Times New Roman', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.CENTER,'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
{'id': 10, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0),'first_line_indent': self.chars_to_twips(2), 'style': 'Normal'},
#二级标题(一)
]
@staticmethod
def chars_to_twips(char_count):
# 估算字符到Twips的转换。这里假设每个字符大约相当于0.5英寸(即720Twips)。
# 这个值可能需要根据实际使用的字体和字号进行调整。
return char_count * 720
# 对段落和运行格式化的函数
def formatit(self, paragraph, format_id):
jsondata = self.getformatjson(format_id)
# 设置段落格式
paragraph.style = jsondata['style']
paragraph.paragraph_format.alignment = jsondata['alignment']
paragraph.paragraph_format.first_line_indent = jsondata['first_line_indent']
# 设置字体格式
for run in paragraph.runs:
run.font.name = jsondata['font']
run.font.size = Pt(jsondata['pt'])
r = run._element.rPr.rFonts
r.set(qn("w:eastAsia"), jsondata['font'])
run.font.color.rgb = jsondata['color']
# 获取格式化数据的函数
def getformatjson(self, format_id):
for w in self.formats:
if w['id'] == format_id:
return w
return {}
当定义完了类之后,就是引用
# 创建Word文档
doc = docx.Document()
sections = doc.sections
section = sections[0]
section.left_margin = 1 * 972000 ## 设置页边距, 1 * 914400 设置1英寸页边距也就是2.54cm,1 * 1332000 =3.7cm。972000=2.7cm;900000= 2.5;1080000 =3cm
section.right_margin = 1 * 900000
section.top_margin = 1 * 1332000
section.bottom_margin = 1 * 1080000
# 设置默认字体和字号
doc.styles['Normal'].font.name = '方正小标宋简体'
doc.styles['Normal'].font.size = Pt(16)
total_manyilv_rounded = round(total_manyilv, 1)
# 添加标题
title = doc.add_heading("这里是标题内容", level=0)
fdoc = fdocx()
fdoc.formatit(title, 5) # 假设标题使用格式5
# 添加累计情况
累计情况 = doc.add_heading("一、累计情况", level=1)
fdoc.formatit(累计情况, 1) # 假设累计情况使用格式3
# 累计情况段落下方添加新内容
content1 = f"你要输入的内容。"
new_paragraph = doc.add_paragraph(content1)
fdoc.formatit(new_paragraph, 6) # 应用格式ID为6的样式
# 在new_content下方再添加一段内容
content2 = f"你要输入的内容。"
additional_paragraph = doc.add_paragraph(content2)
fdoc.formatit(additional_paragraph, 6) # 应用格式ID为7的样式,如果需要不同的格式
output_path3 = output_directory + f"/这里是你的路径"
doc.save(output_path3 + ".docx")
self.textBrowser.append(f"内容")
以上是生成一个word并设置其页边距, 设置页边距, 1 * 914400 设置1英寸页边距也就是2.54cm,1 * 1332000 =3.7cm。972000=2.7cm;900000= 2.5;1080000 =3cm。大家可以根据要求换算。
下面链接是当时参考的,也很详细,可以搭配看https://blog.csdn.net/smdbs2000/article/details/134554518?spm=1001.2014.3001.5506