Python批量调整Word文档中的字体、段落间距及格式python调用函数批量调整word格式

    最近关于批处理格式的问题我查了很多资料,但是都没有找到自己想要的答案。接上期,上篇博文我简单介绍了python操作Word的一些基本操作,本篇重点介绍如何批量将python中的文字导入到Word中,评设置其字体字号、间距、样式等。

Python读写word文档

关键代码

用python 处理docx文档时,想设置首行缩进2字符,有的帖子给出用0.74CM代替,但设置字体后

# 首行缩进0.74厘米,即2个字符
paragraph_format.first_line_indent = Cm(0.74)   
# 换行符
# docx.add_paragraph().add_run('\n')
# 换页符
# docx.add_page_break()
    

一级标题设置 

    直接定义一个函数,设置字体字号、段前断后距,二级三级同理,其中可以把标题看成一个段落。


# 设置1级标题
def heading_1(str_b1):
    heading_1 = docx.add_heading('',level=1)#返回1级标题段落对象,标题也相当于一个段落
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT 
    heading_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 两端对齐 
    heading_1.paragraph_format.space_before=Pt(0.5)#设置段前 0 磅
    heading_1.paragraph_format.space_after=Pt(0.5) #设置段后 0 磅
    heading_1.paragraph_format.line_spacing=1.5 #设置行间距为 1.5
    heading_1.paragraph_format.left_indent=Inches(0)#设置左缩进 1英寸
    heading_1.paragraph_format.right_indent=Inches(0)#设置右缩进 0.5 英寸
    run=heading_1.add_run(str_b1)
    run.font.name=u'宋体'    #设置为宋体
    run.font.name=u'Times New Roman'    #设置为宋体
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#设置为宋体,和上边的一起使用
    run.font.size=Pt(16)#设置1级标题文字的大小为“三号” 为16磅
    run.font.color.rgb=RGBColor(0,0,0)#设置颜色为黑色

 正文设置

       代码都差不多,只是说标题是add_heading;正文是段落add_paragrapha


# 设置正文格式
def text(str):
    paragrapha = docx.add_paragraph(str)
    # 将字体设置为12磅,即小四字体
    paragrapha.style.font.size = Pt(12)
    from docx.shared import Cm
    paragrapha.paragraph_format.first_line_indent = Cm(0.74)
    docx.styles['Normal'].font.name = 'Times New Roman'  
    docx.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') 
    paragrapha.paragraph_format.first_line_indent = 2
    paragrapha.paragraph_format.space_before=Pt(0.5)#设置段前 0 磅
    paragrapha.paragraph_format.space_after=Pt(0.5) #设置段后 0 磅
    paragrapha.paragraph_format.line_spacing=1.15 #设置行间距为 1.5
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT 
    paragrapha.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 两端对齐

完整代码:

# -*- coding: utf-8 -*-
"""
Created on Sun May  7 18:28:34 2023

@author: ypzhao
"""

from docx import Document   #用来建立一个word对象
from docx.shared import Pt  #用来设置字体的大小
from docx.shared import Inches
from docx.oxml.ns import qn  #设置字体
from docx.shared import RGBColor  #设置字体的颜色
from docx.enum.text import WD_ALIGN_PARAGRAPH  #设置对其方式
import matplotlib.pyplot as plt  #导入绘图模块


plt.rcParams.update({'font.family': 'STIXGeneral','mathtext.fontset': 'stix'}) #设置stix字体

docx = Document(r'C:/Users/ypzhao/Desktop/训练/减速器.docx')

def test():
    print("this is a test")
test()

# 换行符
# docx.add_paragraph().add_run('\n')
# 换页符
# docx.add_page_break()


# 设置1级标题
def heading_1(str_b1):
    heading_1 = docx.add_heading('',level=1)#返回1级标题段落对象,标题也相当于一个段落
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT 
    heading_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 两端对齐 
    heading_1.paragraph_format.space_before=Pt(0.5)#设置段前 0 磅
    heading_1.paragraph_format.space_after=Pt(0.5) #设置段后 0 磅
    heading_1.paragraph_format.line_spacing=1.5 #设置行间距为 1.5
    heading_1.paragraph_format.left_indent=Inches(0)#设置左缩进 1英寸
    heading_1.paragraph_format.right_indent=Inches(0)#设置右缩进 0.5 英寸
    run=heading_1.add_run(str_b1)
    run.font.name=u'宋体'    #设置为宋体
    run.font.name=u'Times New Roman'    #设置为宋体
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#设置为宋体,和上边的一起使用
    run.font.size=Pt(16)#设置1级标题文字的大小为“三号” 为16磅
    run.font.color.rgb=RGBColor(0,0,0)#设置颜色为黑色


# 设置2级标题
def heading_2(str_b2):
    heading_2 = docx.add_heading('',level=2)#返回1级标题段落对象,标题也相当于一个段落
    
    heading_2.alignment=WD_ALIGN_PARAGRAPH.LEFT#设置为左对齐

    heading_2.paragraph_format.space_before=Pt(0.5)#设置段前 0 磅
    heading_2.paragraph_format.space_after=Pt(0.5) #设置段后 0 磅
    heading_2.paragraph_format.line_spacing=1.5 #设置行间距为 1.5
    heading_2.paragraph_format.left_indent=Inches(0)#设置左缩进 1英寸
    heading_2.paragraph_format.right_indent=Inches(0)#设置右缩进 0.5 英寸
    run=heading_2.add_run(str_b2)
    run.font.name=u'宋体'    #设置为宋体
    run.font.name=u'Times New Roman'    #设置为宋体
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#设置为宋体,和上边的一起使用
    run.font.size=Pt(15)#设置1级标题文字的大小为“小三号” 为15磅
    run.font.color.rgb=RGBColor(0,0,0)#设置颜色为黑色


# 设置3级标题
def heading_3(str_b3):
    heading_3 = docx.add_heading('',level=3)#返回1级标题段落对象,标题也相当于一个段落
    heading_3.alignment=WD_ALIGN_PARAGRAPH.LEFT#设置为左对齐
    heading_3.paragraph_format.space_before=Pt(0.5)#设置段前 0 磅
    heading_3.paragraph_format.space_after=Pt(0.5) #设置段后 0 磅
    heading_3.paragraph_format.line_spacing=1.5 #设置行间距为 1.5
    heading_3.paragraph_format.left_indent=Inches(0)#设置左缩进 1英寸
    heading_3.paragraph_format.right_indent=Inches(0)#设置右缩进 0.5 英寸
    run=heading_3.add_run(str_b3)
    run.font.name=u'宋体'    #设置为宋体
    run.font.name=u'Times New Roman'    #设置为宋体
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#设置为宋体,和上边的一起使用
    run.font.size=Pt(14)#设置1级标题文字的大小为“四号” 为14磅
    run.font.color.rgb=RGBColor(0,0,0)#设置颜色为黑色


# 设置正文格式
def text(str):
    paragrapha = docx.add_paragraph(str)
    # 将字体设置为12磅,即小四字体
    paragrapha.style.font.size = Pt(12)
    from docx.shared import Cm
    paragrapha.paragraph_format.first_line_indent = Cm(0.74)
    docx.styles['Normal'].font.name = 'Times New Roman'  
    docx.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') 
    paragrapha.paragraph_format.first_line_indent = 2
    paragrapha.paragraph_format.space_before=Pt(0.5)#设置段前 0 磅
    paragrapha.paragraph_format.space_after=Pt(0.5) #设置段后 0 磅
    paragrapha.paragraph_format.line_spacing=1.15 #设置行间距为 1.5
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT 
    paragrapha.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 两端对齐


    # p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 居中对齐  
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 左对齐
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT  # 右对齐
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.DISTRIBUTE  # 分散对齐

str_b1 = "第一部分 设计任务书"
heading_1(str_b1)
str_b2 = "1.1 初始数据"
heading_2(str_b2)   
 
str = ("设计展开式二级直齿圆柱齿轮减速器,初始数据F = 3000N,V = 1.5m/s,D = 250mm,设计年限(寿命):8年,每天工作班制(8小时/班):1班制,每年工作天数:300天,三相交流电源,电压380/220V。")

text(str)

str_b2 = "1.2 设计步骤"
heading_2(str_b2)      

str =("""1.传动装置总体设计方案\n2.电动机的选择,\n3.确定传动装置的总传动比和分配传动比,\n4.计算传动装置的运动和动力参数
5.齿轮的设计
6.滚动轴承和传动轴的设计
7.键联接设计
8.箱体结构设计
9.润滑密封设计
10.联轴器设计
"""
)
text(str)

docx.add_page_break()
str_b1 = "第二部分 传动装置总体设计方案"
heading_1(str_b1)

str_b2 = "2.1 传动方案特点"
heading_2(str_b2)

str = """1.组成:传动装置由电机、减速器、工作机组成。
2.特点:齿轮相对于轴承不对称分布,故沿轴向载荷分布不均匀,要求轴有较大的刚度。
3.确定传动方案:选择电动机-展开式二级直齿圆柱齿轮减速器-工作机。
"""
text(str)

str_b2 = "2.2 计算传动装置总效率"
heading_2(str_b2)

str = """0.993×0.972×0.992×0.96=0.859
1为轴承的效率,2为齿轮啮合传动的效率,3为联轴器的效率,4为工作装置的效率。
"""
text(str)

docx.save('减速器.docx')

运行结果

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年7月11日
下一篇 2023年7月11日

相关推荐