1人参与 • 2025-03-10 • Python
最近关于批处理格式的问题我查了很多资料,但是都没有找到自己想要的答案。接上期,上篇博文我简单介绍了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')
以上就是python批量调整word文档中的字体、段落间距及格式的详细内容,更多关于python调整word格式的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论