
python-docx自定义word段落样式
【代码】python-docx自定义word段落样式。
·
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.style import WD_STYLE_TYPE
# 创建一个新的Word文档对象
doc = Document()
# 添加一个大标题
doc.add_heading('Main Heading', level=1)
# 创建一个独特的自定义段落样式名称
style_name = 'Custom Subheading'
# 创建一个段落样式
style = doc.styles.add_style(style_name, WD_STYLE_TYPE.PARAGRAPH)
style.font.name = 'Arial'
style.font.size = Pt(12)
style.font.bold = True
style.font.color.rgb = RGBColor(0x00, 0x00, 0xFF) # Blue color
# 添加一个小标题,并应用自定义段落样式
paragraph = doc.add_paragraph('Custom Subheading Text')
paragraph.style = style_name # 应用样式
# 设置段落的对齐方式
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# 保存文档
doc.save(r"D:\word_test\example.docx")
更多推荐
所有评论(0)