最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python格式化Word文檔中的文本和段落

 更新時間:2026年03月24日 09:49:49   作者:大丸子  
在創(chuàng)建專業(yè) Word 文檔時,文本和段落的格式化是基礎(chǔ)且重要的技能,本文將介紹如何使用 Python 對 Word 文檔中的文本字符和段落進行各種格式化操作,需要的朋友可以參考下

引言

在創(chuàng)建專業(yè) Word 文檔時,文本和段落的格式化是基礎(chǔ)且重要的技能。良好的格式設(shè)置可以提升文檔的可讀性和專業(yè)性。本文將介紹如何使用 Python 對 Word 文檔中的文本字符和段落進行各種格式化操作。

掌握這些技術(shù)對于生成報告、合同、簡歷等各類文檔都具有重要價值,特別是在需要批量生成格式統(tǒng)一文檔的自動化場景中。

本文方法基于 Free Spire.Doc for Python。

環(huán)境準備

首先需要安裝 Free Spire.Doc for Python 庫:

pip install Spire.Doc.Free

該庫提供了完整的 Word 文檔處理功能,支持文本和段落的各種格式化操作。

字符格式化基礎(chǔ)

字符格式化控制文本的外觀,包括字體、大小、顏色等屬性:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
# 添加不同樣式的文本
paragraph = section.AddParagraph()
# 基本字體設(shè)置
text1 = paragraph.AppendText("這是普通文本。\n")
text1.CharacterFormat.FontName = "微軟雅黑"
text1.CharacterFormat.FontSize = 12
# 粗體和斜體
text2 = paragraph.AppendText("這是粗體文本。\n")
text2.CharacterFormat.Bold = True
text2.CharacterFormat.FontSize = 14
# 斜體文本
text3 = paragraph.AppendText("這是斜體文本。\n")
text3.CharacterFormat.Italic = True
# 粗斜體
text4 = paragraph.AppendText("這是粗斜體文本。\n")
text4.CharacterFormat.Bold = True
text4.CharacterFormat.Italic = True
# 下劃線
text5 = paragraph.AppendText("這是帶下劃線的文本。\n")
text5.CharacterFormat.UnderlineStyle = UnderlineStyle.Single
# 文本顏色
text6 = paragraph.AppendText("這是紅色文本。\n")
text6.CharacterFormat.TextColor = Color.get_Red()
# 高亮顯示
text7 = paragraph.AppendText("這是黃色高亮文本。")
text7.CharacterFormat.HighlightColor = Color.get_Yellow()
document.SaveToFile("character_formatting.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

關(guān)鍵屬性說明:

  • FontName:設(shè)置字體名稱,支持中英文字體
  • FontSize:字體大小,單位為磅(point)
  • Bold / Italic:布爾值,控制粗體和斜體
  • UnderlineStyle:下劃線樣式枚舉
  • TextColor / HighlightColor:文本顏色和背景高亮色

高級字符效果

除了基本格式,還可以應(yīng)用各種特殊文本效果:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
paragraph = section.AddParagraph()
# 刪除線
text1 = paragraph.AppendText("刪除線文本\n")
text1.CharacterFormat.IsStrikeout = True
# 雙刪除線
text2 = paragraph.AppendText("雙刪除線文本\n")
text2.CharacterFormat.DoubleStrike = True
# 陰影效果
text3 = paragraph.AppendText("陰影文本\n")
text3.CharacterFormat.IsShadow = True
# 小大寫字母
text4 = paragraph.AppendText("SMALL CAPS TEXT\n")
text4.CharacterFormat.IsSmallCaps = True
# 全部大寫
text5 = paragraph.AppendText("All Caps Text\n")
text5.CharacterFormat.AllCaps = True
# 上標和下標
text6 = paragraph.AppendText("H?O 和 E=mc2\n")
# 注意:需要分別設(shè)置每個字符的上標/下標
# 輪廓效果
text7 = paragraph.AppendText("輪廓文本\n")
text7.CharacterFormat.IsOutLine = True
# 陽文效果
text8 = paragraph.AppendText("陽文文本\n")
text8.CharacterFormat.Emboss = True
# 陰文效果
text9 = paragraph.AppendText("陰文文本\n")
text9.CharacterFormat.Engrave = True
# 隱藏文本
text10 = paragraph.AppendText("可見文本")
hidden = paragraph.AppendText(" [隱藏內(nèi)容]")
hidden.CharacterFormat.Hidden = True
document.SaveToFile("text_effects.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

這些特殊效果適用于特定場景:

  • 刪除線:標記已刪除或過時的內(nèi)容
  • 上標/下標:數(shù)學(xué)公式、化學(xué)式
  • 隱藏文本:添加備注或注釋
  • 陽文/陰文:特殊印刷效果

段落對齊方式

段落對齊控制文本在頁面上的水平位置:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
# 左對齊
para1 = section.AddParagraph()
para1.AppendText("這是左對齊的段落。")
para1.Format.HorizontalAlignment = HorizontalAlignment.Left
# 居中對齊
para2 = section.AddParagraph()
para2.AppendText("這是居中對齊的段落。")
para2.Format.HorizontalAlignment = HorizontalAlignment.Center
# 右對齊
para3 = section.AddParagraph()
para3.AppendText("這是右對齊的段落。")
para3.Format.HorizontalAlignment = HorizontalAlignment.Right
# 兩端對齊
para4 = section.AddParagraph()
para4.AppendText("這是兩端對齊的段落,文本會在左右邊界之間均勻分布,使段落看起來更加整齊。" * 3)
para4.Format.HorizontalAlignment = HorizontalAlignment.Justify
# 分散對齊
para5 = section.AddParagraph()
para5.AppendText("分散對齊示例")
para5.Format.HorizontalAlignment = HorizontalAlignment.Distribute
document.SaveToFile("paragraph_alignment.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

段落縮進設(shè)置

縮進用于控制段落與頁面邊界的距離:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
# 首行縮進(中文文檔常用)
para1 = section.AddParagraph()
para1.AppendText("這是首行縮進的段落。在中文文檔中,通常每個自然段的首行會縮進兩個字符的位置,這是傳統(tǒng)的排版習(xí)慣。")
para1.Format.SetFirstLineIndent(28)  # 約等于兩個字符寬度
# 懸掛縮進
para2 = section.AddParagraph()
para2.AppendText("這是懸掛縮進的段落。懸掛縮進是指除首行外的其他行都縮進,常用于參考文獻列表或項目清單。")
para2.Format.SetFirstLineIndent(-28)  # 負值表示懸掛縮進
# 左縮進
para3 = section.AddParagraph()
para3.AppendText("這個段落整體向右縮進。左縮進將整個段落向右移動,適用于引用文本或需要特別標注的內(nèi)容。")
para3.Format.SetLeftIndent(40)
# 右縮進
para4 = section.AddParagraph()
para4.AppendText("這個段落右側(cè)也進行了縮進。通過同時設(shè)置左右縮進,可以創(chuàng)建一個居中但不對稱的文本塊效果。")
para4.Format.SetLeftIndent(40)
para4.Format.SetRightIndent(40)
document.SaveToFile("paragraph_indentation.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

縮進類型說明:

  • 首行縮進:僅第一行縮進,正值
  • 懸掛縮進:除第一行外都縮進,負值
  • 左/右縮進:整個段落的左右邊界調(diào)整

段落間距和行距

控制段落之間和段落內(nèi)部的垂直間距:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
title = section.AddParagraph()
title.AppendText("段落間距和行距示例")
title.ApplyStyle(BuiltinStyle.Heading1)
# 段前間距
para1 = section.AddParagraph()
para1.AppendText("這個段落上方有較大的間距。")
para1.Format.BeforeSpacing = 20  # 段前 20 磅
# 段后間距
para2 = section.AddParagraph()
para2.AppendText("這個段落下方有較大的間距。")
para2.Format.AfterSpacing = 20  # 段后 20 磅
# 固定行距
para3 = section.AddParagraph()
para3.AppendText("這是固定行距的文本內(nèi)容。\n第二行文本。\n第三行文本。")
para3.Format.LineSpacingRule = LineSpacingRule.Exactly
para3.Format.LineSpacing = 24  # 固定 24 磅行距
# 最小行距
para4 = section.AddParagraph()
para4.AppendText("這是最小行距的文本內(nèi)容。\n第二行文本。\n第三行文本。")
para4.Format.LineSpacingRule = LineSpacingRule.AtLeast
para4.Format.LineSpacing = 18  # 至少 18 磅,可根據(jù)字體大小自動調(diào)整
# 倍數(shù)行距
para5 = section.AddParagraph()
para5.AppendText("這是 1.5 倍行距的文本內(nèi)容。\n第二行文本。\n第三行文本。")
para5.Format.LineSpacingRule = LineSpacingRule.Multiple
para5.Format.LineSpacing = 1.5  # 1.5 倍行距
document.SaveToFile("paragraph_spacing.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

行距規(guī)則對比:

  • Exactly:固定值,不隨字體大小變化
  • AtLeast:最小值,可根據(jù)內(nèi)容擴展
  • Multiple:倍數(shù)關(guān)系(如 1.5 倍、2 倍)

段落邊框和底紋

為段落添加邊框和背景色:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
# 四邊邊框
para1 = section.AddParagraph()
para1.AppendText("這個段落帶有四邊邊框。")
para1.Format.Borders.BorderType = BorderStyle.Single
para1.Format.Borders.Color = Color.get_Blue()
para1.Format.Borders.LineWidth = 1.0
# 單獨設(shè)置各邊邊框
para2 = section.AddParagraph()
para2.AppendText("這個段落只有上下邊框。")
para2.Format.Borders.Top.BorderType = BorderStyle.Double
para2.Format.Borders.Top.Color = Color.get_Red()
para2.Format.Borders.Bottom.BorderType = BorderStyle.Single
para2.Format.Borders.Bottom.Color = Color.get_Green()
# 添加背景底紋
para3 = section.AddParagraph()
para3.AppendText("這個段落有灰色背景。")
para3.Format.BackColor = Color.get_LightGray()
# 組合效果
para4 = section.AddParagraph()
para4.AppendText("邊框 + 底紋的組合效果。")
para4.Format.Borders.BorderType = BorderStyle.Single
para4.Format.Borders.Color = Color.get_DarkBlue()
para4.Format.BackColor = Color.get_LightYellow()
document.SaveToFile("paragraph_borders.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

使用內(nèi)置樣式

Word 提供了豐富的內(nèi)置樣式,可以快速應(yīng)用專業(yè)格式:

from spire.doc import *
from spire.doc.common import *
document = Document()
section = document.AddSection()
# 標題樣式
title = section.AddParagraph()
title.AppendText("主標題")
title.ApplyStyle(BuiltinStyle.Title)
# 各級標題
heading1 = section.AddParagraph()
heading1.AppendText("一級標題")
heading1.ApplyStyle(BuiltinStyle.Heading1)
heading2 = section.AddParagraph()
heading2.AppendText("二級標題")
heading2.ApplyStyle(BuiltinStyle.Heading2)
heading3 = section.AddParagraph()
heading3.AppendText("三級標題")
heading3.ApplyStyle(BuiltinStyle.Heading3)
# 正文樣式
normal = section.AddParagraph()
normal.AppendText("這是普通正文樣式。")
normal.ApplyStyle(BuiltinStyle.Normal)
# 引用樣式
quote = section.AddParagraph()
quote.AppendText("這是引用樣式,適用于名人名言或重要引文。")
quote.ApplyStyle(BuiltinStyle.CommentReference)
# 列表段落樣式
listPara = section.AddParagraph()
listPara.AppendText("列表項內(nèi)容")
listPara.ApplyStyle(BuiltinStyle.ListBullet)
document.SaveToFile("builtin_styles.docx", FileFormat.Docx)
document.Close()

結(jié)果文檔:

常用內(nèi)置樣式:

  • Title / Heading1-9:標題層級
  • Normal:標準正文
  • CommentReference:引用
  • ListBullet:列表項

綜合示例:格式化商業(yè)信函

下面是一個完整的商業(yè)信函格式化示例:

from spire.doc import *
from spire.doc.common import *
def create_business_letter():
    document = Document()
    section = document.AddSection()
    # 設(shè)置頁面邊距
    section.PageSetup.LeftMargin = 72  # 1 英寸
    section.PageSetup.RightMargin = 72
    # 公司信頭(居中,大號字體)
    header = section.AddParagraph()
    company_name = header.AppendText("某某科技有限公司")
    company_name.CharacterFormat.FontName = "微軟雅黑"
    company_name.CharacterFormat.FontSize = 18
    company_name.CharacterFormat.Bold = True
    header.Format.HorizontalAlignment = HorizontalAlignment.Center
    header.Format.AfterSpacing = 10
    # 聯(lián)系信息(居中,小號字體)
    contact = section.AddParagraph()
    contact_text = contact.AppendText("地址:北京市朝陽區(qū)某某路 123 號 | 電話:010-12345678 | 郵箱:info@company.com")
    contact_text.CharacterFormat.FontSize = 10
    contact_text.CharacterFormat.TextColor = Color.get_DarkGray()
    contact.Format.HorizontalAlignment = HorizontalAlignment.Center
    contact.Format.AfterSpacing = 30
    # 日期(右對齊)
    date_para = section.AddParagraph()
    from datetime import datetime
    date_para.AppendText(f"日期:{datetime.now().strftime('%Y年%m月%d日')}")
    date_para.Format.HorizontalAlignment = HorizontalAlignment.Right
    date_para.Format.AfterSpacing = 20
    # 收件人信息(左對齊,加粗)
    recipient = section.AddParagraph()
    recipient_text = recipient.AppendText("尊敬的客戶:\n")
    recipient_text.CharacterFormat.Bold = True
    recipient.Format.AfterSpacing = 20
    # 正文段落(首行縮進,兩端對齊)
    body1 = section.AddParagraph()
    body1.AppendText("感謝您一直以來對我們公司的支持與信任。我們非常重視與您的合作關(guān)系,并致力于為您提供最優(yōu)質(zhì)的產(chǎn)品和服務(wù)。")
    body1.Format.SetFirstLineIndent(28)
    body1.Format.HorizontalAlignment = HorizontalAlignment.Justify
    body1.Format.LineSpacingRule = LineSpacingRule.Multiple
    body1.Format.LineSpacing = 15
    body1.Format.AfterSpacing = 20
    body2 = section.AddParagraph()
    body2.AppendText("為了進一步提升服務(wù)質(zhì)量,我們將于下個月推出全新的客戶服務(wù)計劃。該計劃將為您提供更多個性化選擇和專屬優(yōu)惠。我們相信,通過這些改進,我們的合作將更加緊密和愉快。")
    body2.Format.SetFirstLineIndent(28)
    body2.Format.HorizontalAlignment = HorizontalAlignment.Justify
    body2.Format.LineSpacingRule = LineSpacingRule.Multiple
    body2.Format.LineSpacing = 15
    body2.Format.AfterSpacing = 20
    # 結(jié)束語(右對齊)
    closing = section.AddParagraph()
    closing.AppendText("此致\n敬禮\n\n")
    closing.Format.HorizontalAlignment = HorizontalAlignment.Right
    closing.Format.AfterSpacing = 10
    # 簽名區(qū)域
    signature = section.AddParagraph()
    sig_name_text = sig_name = signature.AppendText("張三\n")
    sig_name_text.CharacterFormat.Bold = True
    signature.AppendText("客戶經(jīng)理\n某某科技有限公司")
    signature.Format.SetFirstLineIndent(28)
    document.SaveToFile("business_letter.docx", FileFormat.Docx)
    document.Close()
# 執(zhí)行
create_business_letter()

運行結(jié)果:

總結(jié)

本文全面介紹了使用 Python 格式化 Word 文檔中文字和段落的各種技術(shù)。主要內(nèi)容包括:

  • 字符格式化:字體、大小、顏色、特殊效果
  • 段落對齊:左對齊、居中、右對齊、兩端對齊、分散對齊
  • 段落縮進:首行縮進、懸掛縮進、左右縮進
  • 間距控制:段前距、段后距、行距規(guī)則
  • 裝飾效果:邊框、底紋、背景色
  • 內(nèi)置樣式:快速應(yīng)用專業(yè)格式

掌握這些格式化技術(shù)后,開發(fā)者可以創(chuàng)建出格式規(guī)范、外觀專業(yè)的各類 Word 文檔。在實際應(yīng)用中,應(yīng)根據(jù)文檔類型和用途選擇合適的格式化組合,既要保證可讀性,也要考慮美觀性和專業(yè)性。

通過程序化設(shè)置格式,可以實現(xiàn)批量文檔的自動化生成,確保所有文檔保持一致的格式標準,大大提高文檔處理效率。

以上就是使用Python格式化Word文檔中的文本和段落的詳細內(nèi)容,更多關(guān)于Python格式化Word文本和段落的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python中map、any、all函數(shù)用法分析

    python中map、any、all函數(shù)用法分析

    這篇文章主要介紹了python中map、any、all函數(shù)用法,實例分析了map、any、all函數(shù)的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • python交互式圖形編程實例(三)

    python交互式圖形編程實例(三)

    這篇文章主要為大家詳細介紹了python交互式圖形編程實例第三篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • python對html過濾處理的方法

    python對html過濾處理的方法

    今天小編就為大家分享一篇python對html過濾處理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面,并展示

    Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面,并展示

    這篇文章主要介紹了Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面并展示,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python?中collections的?deque使用詳解

    python?中collections的?deque使用詳解

    這篇文章主要介紹了python中collections的deque使用詳解,deque是一個雙端隊列,如果要經(jīng)常從兩端append的數(shù)據(jù),選擇這個數(shù)據(jù)結(jié)構(gòu)就比較好了,更多相關(guān)內(nèi)容,需要的小伙伴可以參考下面文章內(nèi)容
    2022-09-09
  • Python?socket如何解析HTTP請求內(nèi)容

    Python?socket如何解析HTTP請求內(nèi)容

    這篇文章主要介紹了Python?socket如何解析HTTP請求內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)

    Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)

    本文主要介紹了Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 基于Python實現(xiàn)png轉(zhuǎn)webp的命令行工具

    基于Python實現(xiàn)png轉(zhuǎn)webp的命令行工具

    網(wǎng)頁上使用webp格式的圖片更加省網(wǎng)絡(luò)流量和存儲空間,但本地圖片一般是png格式的,所以本文就來為大家介紹一下如何使用Python實現(xiàn)png轉(zhuǎn)webp功能吧
    2025-02-02
  • python+PyQt5 左右聲道測試源代碼

    python+PyQt5 左右聲道測試源代碼

    這篇文章主要介紹了python+PyQt5 左右聲道測試源代碼,左聲道,人機交互測試,點擊右邊聽到的對應(yīng)序號按鈕,對python左右聲道測試感興趣的朋友一起看看吧
    2024-02-02
  • 基于Python編寫一個打印機批量打印隊列工具

    基于Python編寫一個打印機批量打印隊列工具

    有時候我們在批量打印文件的時候,總會遇到電腦上打印機隊列打不開的情況,為此我們可以利用Python寫一個打印機批量打印隊列,下面小編就來和大家詳細講講吧
    2025-02-02

最新評論

凭祥市| 陆河县| 定西市| 宽甸| 吴堡县| 昭觉县| 诸暨市| 海南省| 九龙县| 临沂市| 玛曲县| 鄱阳县| 申扎县| 绥滨县| 澄江县| 东光县| 平南县| 桃园市| 嵊州市| 全椒县| 武胜县| 英超| 余干县| 赞皇县| 德惠市| 八宿县| 类乌齐县| 岚皋县| 平江县| 密山市| 迭部县| 忻城县| 合川市| 防城港市| 兖州市| 嘉鱼县| 肥西县| 蒙城县| 乐安县| 武义县| 邛崃市|