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')

运行结果

  • 12
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: 可以使用 python-docx 库来实现调整整页段落间距。具体实现方法可以参考以下代码: ```python import docx doc = docx.Document('your_document.docx') # 打开需要调整段落间距文档 for para in doc.paragraphs: para_format = para.paragraph_format para_format.space_before = docx.shared.Pt() # 设置段前间距为 para_format.space_after = docx.shared.Pt(12) # 设置段后间距为12磅 doc.save('new_document.docx') # 保存修改后的文档 ``` 其,`space_before` 和 `space_after` 分别表示段落前后的间距,单位为磅(Pt)。以上代码将段前间距设置为,段后间距设置为12磅,可以根据需要进行调整。 ### 回答2: Python可以使用python-docx库来操作Word文档,并且可以调整整页段落间距。下面是一个简单的示例代码: ```python from docx import Document from docx.shared import Pt # 打开Word文档 doc = Document('example.docx') # 获取所有段落 paragraphs = doc.paragraphs # 遍历所有段落 for paragraph in paragraphs: # 设置段落间距为1.5倍行距 paragraph.paragraph_format.line_spacing = 1.5 # 保存修改后的文档 doc.save('updated_example.docx') ``` 以上代码,首先使用`Document`函数打开Word文档,并将其赋值给变量`doc`。然后使用`doc.paragraphs`获取文档的所有段落,保存在变量`paragraphs`。 接下来,通过遍历每个段落,可以使用`paragraph.paragraph_format.line_spacing`属性来设置段落的行距。示例将行距设置为1.5倍,可以根据需要进行调整。 最后,使用`doc.save`保存修改后的文档,并指定保存的文件名。 这样,通过以上代码就可以使用python-docx库来调整Word文档整页段落间距。注意要先安装python-docx库,可以使用`pip install python-docx`命令进行安装。 ### 回答3: 在Python,使用python-docx库可以很方便地对Word文档进行操作,包括调整整页段落间距。 首先,需要安装python-docx库,可以使用以下命令进行安装: ``` pip install python-docx ``` 接下来,引入相关的库和模块: ```python from docx import Document from docx.shared import Pt ``` 然后,打开需要操作的Word文档: ```python doc = Document('example.docx') # 替换为你需要操作的文档路径 ``` 接下来,可以通过遍历文档段落,对每个段落进行整页段落间距调整: ```python for paragraph in doc.paragraphs: paragraph.paragraph_format.space_after = Pt(12) # 设置段落后的间距为12pt ``` 最后,保存修改后的文档: ```python doc.save('example_modified.docx') # 将修改后的文档保存为新的文件 ``` 以上代码将会将文档所有段落的后间距设置为12pt,并保存为新的文件。你可以根据实际需求,调整代码的参数来实现自定义的间距值。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群智能算法小狂人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值