python-word学习

  1. CSDN的编辑器被看上了,有空研究研究,文字编辑挺好
  2. 段落内容特性部分
##########内容特有的特性##########
p2=document.add_paragraph()
r2=p2.add_run('这是第二段 ABCabc')

#r2.underline=True       #下划线
r2.bold=True            #加粗
r2.font.size=Pt(14)
###############字体设置##########
r2.font.name='黑体'
r2._element.rPr.rFonts.set(qn('w:eastAsia'),'黑体')
r2.font.name='Times New Roman'
  1. 缩进方式试了很久,感觉网上的有问题
#左缩进
p.left_indent = Inches(0.3)

#首行缩进
p.first_line_indent = Inches(0.3)

#上行间距
p.space_before = Pt(18)

#下行间距
p.space_after = Pt(12)

#添加标题
document.add_heading('The REAL meaning of the universe')
document.add_heading('The role of dolphins', level = 2)

我改为:

p2=document.add_paragraph()
r2=p2.add_run('这是第二段 ABCabc')

#r2.underline=True       #下划线
#r2.bold=True            #加粗
r2.font.size=Pt(12)        #小四为12,四号为14,五号10.5
###############字体设置##########
r2.font.name='仿宋'
r2._element.rPr.rFonts.set(qn('w:eastAsia'),'仿宋')
r2.font.name='Times New Roman'

p2.alignment=WD_ALIGN_PARAGRAPH.LEFT
#p2.paragraph_format.left_indent=Inches(0.3)
p2.paragraph_format.first_line_indent=Pt(24)    #首行缩进2字符,实际按字体乘以2

网上的少了paragraph_fomat.这个中间函数,导致实际没效果,加上后正常,另外docx中没有对首行缩进2字符的常用表述,实际按照中文的字号对应pt值,缩进2字符使用first_line_indent=Pt,字号pt值*2即可实现首行缩进2字符的效果。

将几种常用的段落文字格式写成一个包,再来实现自动文档效果。

发布了12 篇原创文章 · 获赞 0 · 访问量 147

猜你喜欢

转载自blog.csdn.net/weixin_37281967/article/details/104056999