欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python批量調(diào)整Word文檔中的字體、段落間距及格式

 更新時(shí)間:2025年03月10日 10:32:19   作者:算法小狂人  
這篇文章主要為大家詳細(xì)介紹了如何使用Python的docx庫來批量處理Word文檔,包括設(shè)置首行縮進(jìn)、字體、字號(hào)、行間距、段落對(duì)齊方式等,需要的可以了解下

最近關(guān)于批處理格式的問題我查了很多資料,但是都沒有找到自己想要的答案。接上期,上篇博文我簡單介紹了python操作Word的一些基本操作,本篇重點(diǎn)介紹如何批量將python中的文字導(dǎo)入到Word中,評(píng)設(shè)置其字體字號(hào)、間距、樣式等。

關(guān)鍵代碼

用python 處理docx文檔時(shí),想設(shè)置首行縮進(jìn)2字符,有的帖子給出用0.74CM代替,但設(shè)置字體后

# 首行縮進(jìn)0.74厘米,即2個(gè)字符
paragraph_format.first_line_indent = Cm(0.74)   
# 換行符
# docx.add_paragraph().add_run('\n')
# 換頁符
# docx.add_page_break()

一級(jí)標(biāo)題設(shè)置 

直接定義一個(gè)函數(shù),設(shè)置字體字號(hào)、段前斷后距,二級(jí)三級(jí)同理,其中可以把標(biāo)題看成一個(gè)段落。

# 設(shè)置1級(jí)標(biāo)題
def heading_1(str_b1):
    heading_1 = docx.add_heading('',level=1)#返回1級(jí)標(biāo)題段落對(duì)象,標(biāo)題也相當(dāng)于一個(gè)段落
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT 
    heading_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 兩端對(duì)齊 
    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è)置左縮進(jìn) 1英寸
    heading_1.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進(jìn) 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級(jí)標(biāo)題文字的大小為“三號(hào)” 為16磅
    run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色

 正文設(shè)置

代碼都差不多,只是說標(biāo)題是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  # 兩端對(duì)齊

完整代碼

# -*- coding: utf-8 -*-
"""
Created on Sun May  7 18:28:34 2023
@author: ypzhao
"""
 
from docx import Document   #用來建立一個(gè)word對(duì)象
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è)置對(duì)其方式
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級(jí)標(biāo)題
def heading_1(str_b1):
    heading_1 = docx.add_heading('',level=1)#返回1級(jí)標(biāo)題段落對(duì)象,標(biāo)題也相當(dāng)于一個(gè)段落
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT 
    heading_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 兩端對(duì)齊 
    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è)置左縮進(jìn) 1英寸
    heading_1.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進(jìn) 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級(jí)標(biāo)題文字的大小為“三號(hào)” 為16磅
    run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色
 
 
# 設(shè)置2級(jí)標(biāo)題
def heading_2(str_b2):
    heading_2 = docx.add_heading('',level=2)#返回1級(jí)標(biāo)題段落對(duì)象,標(biāo)題也相當(dāng)于一個(gè)段落
    
    heading_2.alignment=WD_ALIGN_PARAGRAPH.LEFT#設(shè)置為左對(duì)齊
 
    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è)置左縮進(jìn) 1英寸
    heading_2.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進(jìn) 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級(jí)標(biāo)題文字的大小為“小三號(hào)” 為15磅
    run.font.color.rgb=RGBColor(0,0,0)#設(shè)置顏色為黑色
 
 
# 設(shè)置3級(jí)標(biāo)題
def heading_3(str_b3):
    heading_3 = docx.add_heading('',level=3)#返回1級(jí)標(biāo)題段落對(duì)象,標(biāo)題也相當(dāng)于一個(gè)段落
    heading_3.alignment=WD_ALIGN_PARAGRAPH.LEFT#設(shè)置為左對(duì)齊
    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è)置左縮進(jìn) 1英寸
    heading_3.paragraph_format.right_indent=Inches(0)#設(shè)置右縮進(jìn) 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級(jí)標(biāo)題文字的大小為“四號(hào)” 為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  # 兩端對(duì)齊
 
 
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 居中對(duì)齊  
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 左對(duì)齊
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT  # 右對(duì)齊
    # p.alignment = WD_PARAGRAPH_ALIGNMENT.DISTRIBUTE  # 分散對(duì)齊
 
str_b1 = "第一部分 設(shè)計(jì)任務(wù)書"
heading_1(str_b1)
str_b2 = "1.1 初始數(shù)據(jù)"
heading_2(str_b2)   
 
str = ("設(shè)計(jì)展開式二級(jí)直齒圓柱齒輪減速器,初始數(shù)據(jù)F = 3000N,V = 1.5m/s,D = 250mm,設(shè)計(jì)年限(壽命):8年,每天工作班制(8小時(shí)/班):1班制,每年工作天數(shù):300天,三相交流電源,電壓380/220V。")
 
text(str)
 
str_b2 = "1.2 設(shè)計(jì)步驟"
heading_2(str_b2)      
 
str =("""1.傳動(dòng)裝置總體設(shè)計(jì)方案\n2.電動(dòng)機(jī)的選擇,\n3.確定傳動(dòng)裝置的總傳動(dòng)比和分配傳動(dòng)比,\n4.計(jì)算傳動(dòng)裝置的運(yùn)動(dòng)和動(dòng)力參數(shù)
5.齒輪的設(shè)計(jì)
6.滾動(dòng)軸承和傳動(dòng)軸的設(shè)計(jì)
7.鍵聯(lián)接設(shè)計(jì)
8.箱體結(jié)構(gòu)設(shè)計(jì)
9.潤滑密封設(shè)計(jì)
10.聯(lián)軸器設(shè)計(jì)
"""
)
text(str)
 
docx.add_page_break()
str_b1 = "第二部分 傳動(dòng)裝置總體設(shè)計(jì)方案"
heading_1(str_b1)
 
str_b2 = "2.1 傳動(dòng)方案特點(diǎn)"
heading_2(str_b2)
 
str = """1.組成:傳動(dòng)裝置由電機(jī)、減速器、工作機(jī)組成。
2.特點(diǎn):齒輪相對(duì)于軸承不對(duì)稱分布,故沿軸向載荷分布不均勻,要求軸有較大的剛度。
3.確定傳動(dòng)方案:選擇電動(dòng)機(jī)-展開式二級(jí)直齒圓柱齒輪減速器-工作機(jī)。
"""
text(str)
 
str_b2 = "2.2 計(jì)算傳動(dòng)裝置總效率"
heading_2(str_b2)
 
str = """0.993×0.972×0.992×0.96=0.859
?1為軸承的效率,?2為齒輪嚙合傳動(dòng)的效率,?3為聯(lián)軸器的效率,?4為工作裝置的效率。
"""
text(str)
 
docx.save('減速器.docx')

運(yùn)行結(jié)果

以上就是Python批量調(diào)整Word文檔中的字體、段落間距及格式的詳細(xì)內(nèi)容,更多關(guān)于Python調(diào)整Word格式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論