Python批量調(diào)整Word文檔中的字體、段落間距及格式
最近關(guān)于批處理格式的問題我查了很多資料,但是都沒有找到自己想要的答案。接上期,上篇博文我簡單介紹了python操作Word的一些基本操作,本篇重點介紹如何批量將python中的文字導(dǎo)入到Word中,評設(shè)置其字體字號、間距、樣式等。
關(guān)鍵代碼
用python 處理docx文檔時,想設(shè)置首行縮進2字符,有的帖子給出用0.74CM代替,但設(shè)置字體后
# 首行縮進0.74厘米,即2個字符
paragraph_format.first_line_indent = Cm(0.74)
# 換行符
# docx.add_paragraph().add_run('\n')
# 換頁符
# docx.add_page_break()
一級標題設(shè)置
直接定義一個函數(shù),設(shè)置字體字號、段前斷后距,二級三級同理,其中可以把標題看成一個段落。
# 設(shè)置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)#設(shè)置段前 0 磅
heading_1.paragraph_format.space_after=Pt(0.5) #設(shè)置段后 0 磅
heading_1.paragraph_format.line_spacing=1.5 #設(shè)置行間距為 1.5
heading_1.paragraph_format.left_indent=Inches(0)#設(shè)置左縮進 1英寸
heading_1.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進 0.5 英寸
run=heading_1.add_run(str_b1)
run.font.name=u'宋體' #設(shè)置為宋體
run.font.name=u'Times New Roman' #設(shè)置為宋體
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#設(shè)置為宋體,和上邊的一起使用
run.font.size=Pt(16)#設(shè)置1級標題文字的大小為“三號” 為16磅
run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色正文設(shè)置
代碼都差不多,只是說標題是add_heading;正文是段落add_paragrapha
# 設(shè)置正文格式
def text(str):
paragrapha = docx.add_paragraph(str)
# 將字體設(shè)置為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)#設(shè)置段前 0 磅
paragrapha.paragraph_format.space_after=Pt(0.5) #設(shè)置段后 0 磅
paragrapha.paragraph_format.line_spacing=1.15 #設(shè)置行間距為 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 #用來設(shè)置字體的大小
from docx.shared import Inches
from docx.oxml.ns import qn #設(shè)置字體
from docx.shared import RGBColor #設(shè)置字體的顏色
from docx.enum.text import WD_ALIGN_PARAGRAPH #設(shè)置對其方式
import matplotlib.pyplot as plt #導(dǎo)入繪圖模塊
plt.rcParams.update({'font.family': 'STIXGeneral','mathtext.fontset': 'stix'}) #設(shè)置stix字體
docx = Document(r'C:/Users/ypzhao/Desktop/訓(xùn)練/減速器.docx')
def test():
print("this is a test")
test()
# 換行符
# docx.add_paragraph().add_run('\n')
# 換頁符
# docx.add_page_break()
# 設(shè)置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)#設(shè)置段前 0 磅
heading_1.paragraph_format.space_after=Pt(0.5) #設(shè)置段后 0 磅
heading_1.paragraph_format.line_spacing=1.5 #設(shè)置行間距為 1.5
heading_1.paragraph_format.left_indent=Inches(0)#設(shè)置左縮進 1英寸
heading_1.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進 0.5 英寸
run=heading_1.add_run(str_b1)
run.font.name=u'宋體' #設(shè)置為宋體
run.font.name=u'Times New Roman' #設(shè)置為宋體
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#設(shè)置為宋體,和上邊的一起使用
run.font.size=Pt(16)#設(shè)置1級標題文字的大小為“三號” 為16磅
run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色
# 設(shè)置2級標題
def heading_2(str_b2):
heading_2 = docx.add_heading('',level=2)#返回1級標題段落對象,標題也相當于一個段落
heading_2.alignment=WD_ALIGN_PARAGRAPH.LEFT#設(shè)置為左對齊
heading_2.paragraph_format.space_before=Pt(0.5)#設(shè)置段前 0 磅
heading_2.paragraph_format.space_after=Pt(0.5) #設(shè)置段后 0 磅
heading_2.paragraph_format.line_spacing=1.5 #設(shè)置行間距為 1.5
heading_2.paragraph_format.left_indent=Inches(0)#設(shè)置左縮進 1英寸
heading_2.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進 0.5 英寸
run=heading_2.add_run(str_b2)
run.font.name=u'宋體' #設(shè)置為宋體
run.font.name=u'Times New Roman' #設(shè)置為宋體
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#設(shè)置為宋體,和上邊的一起使用
run.font.size=Pt(15)#設(shè)置1級標題文字的大小為“小三號” 為15磅
run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色
# 設(shè)置3級標題
def heading_3(str_b3):
heading_3 = docx.add_heading('',level=3)#返回1級標題段落對象,標題也相當于一個段落
heading_3.alignment=WD_ALIGN_PARAGRAPH.LEFT#設(shè)置為左對齊
heading_3.paragraph_format.space_before=Pt(0.5)#設(shè)置段前 0 磅
heading_3.paragraph_format.space_after=Pt(0.5) #設(shè)置段后 0 磅
heading_3.paragraph_format.line_spacing=1.5 #設(shè)置行間距為 1.5
heading_3.paragraph_format.left_indent=Inches(0)#設(shè)置左縮進 1英寸
heading_3.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進 0.5 英寸
run=heading_3.add_run(str_b3)
run.font.name=u'宋體' #設(shè)置為宋體
run.font.name=u'Times New Roman' #設(shè)置為宋體
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Times New Roman')#設(shè)置為宋體,和上邊的一起使用
run.font.size=Pt(14)#設(shè)置1級標題文字的大小為“四號” 為14磅
run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色
# 設(shè)置正文格式
def text(str):
paragrapha = docx.add_paragraph(str)
# 將字體設(shè)置為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)#設(shè)置段前 0 磅
paragrapha.paragraph_format.space_after=Pt(0.5) #設(shè)置段后 0 磅
paragrapha.paragraph_format.line_spacing=1.15 #設(shè)置行間距為 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 = "第一部分 設(shè)計任務(wù)書"
heading_1(str_b1)
str_b2 = "1.1 初始數(shù)據(jù)"
heading_2(str_b2)
str = ("設(shè)計展開式二級直齒圓柱齒輪減速器,初始數(shù)據(jù)F = 3000N,V = 1.5m/s,D = 250mm,設(shè)計年限(壽命):8年,每天工作班制(8小時/班):1班制,每年工作天數(shù):300天,三相交流電源,電壓380/220V。")
text(str)
str_b2 = "1.2 設(shè)計步驟"
heading_2(str_b2)
str =("""1.傳動裝置總體設(shè)計方案\n2.電動機的選擇,\n3.確定傳動裝置的總傳動比和分配傳動比,\n4.計算傳動裝置的運動和動力參數(shù)
5.齒輪的設(shè)計
6.滾動軸承和傳動軸的設(shè)計
7.鍵聯(lián)接設(shè)計
8.箱體結(jié)構(gòu)設(shè)計
9.潤滑密封設(shè)計
10.聯(lián)軸器設(shè)計
"""
)
text(str)
docx.add_page_break()
str_b1 = "第二部分 傳動裝置總體設(shè)計方案"
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為聯(lián)軸器的效率,?4為工作裝置的效率。
"""
text(str)
docx.save('減速器.docx')運行結(jié)果

以上就是Python批量調(diào)整Word文檔中的字體、段落間距及格式的詳細內(nèi)容,更多關(guān)于Python調(diào)整Word格式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python 讀取excel并轉(zhuǎn)換為字典的方法
文章介紹了兩種方法使用Python讀取Excel文件并將其轉(zhuǎn)換為字典,方法一使用xlrd庫,方法二使用自定義的xToolkit庫,感興趣的朋友一起看看吧2025-03-03
pyqt彈出新對話框,以及關(guān)閉對話框獲取數(shù)據(jù)的實例
今天小編就為大家分享一篇pyqt彈出新對話框,以及關(guān)閉對話框獲取數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
利用Python實現(xiàn)一個可定制風(fēng)格的繪圖系統(tǒng)
這篇文章主要為大家詳細介紹了如何基于Python實現(xiàn)一個可定制風(fēng)格的繪圖系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解下2023-09-09

