Python實現(xiàn)在Word中創(chuàng)建,讀取和刪除列表詳解
在Word中,列表是一種用于組織和呈現(xiàn)信息的有效工具。它們不僅可以幫助用戶清晰地展示數(shù)據(jù)、概念或步驟,還能增強(qiáng)文檔的可讀性和專業(yè)性。通過使用列表,用戶能夠更直觀地理解信息之間的關(guān)系,尤其是在處理復(fù)雜內(nèi)容時。在這篇博客中,我們將探討如何使用Python在Word文檔中創(chuàng)建、讀取和刪除列表,主要涉及的內(nèi)容如下:
- Python在Word中創(chuàng)建列表
- 使用默認(rèn)樣式創(chuàng)建有序(編號)列表
- 使用默認(rèn)樣式創(chuàng)建無序(項目符號)列表
- 創(chuàng)建多級列表
- 使用自定義樣式創(chuàng)建列表
- Python讀取Word中的列表
- Python從Word中刪除列表
工具與設(shè)置
要使用Python在Word文檔中創(chuàng)建和操作列表,可以使用Free Spire.Doc for Python庫。它是一個免費(fèi)的Word文檔處理庫,主要用于在Python應(yīng)用程序中生成、讀取、編輯和轉(zhuǎn)換Word文檔。
在開始之前,請通過以下命令安裝該庫:
pip install Spire.Doc.Free
Python在Word中創(chuàng)建列表
Microsoft Word提供了多種列表選項,包括有序(編號)列表、無序(項目符號)列表和多級列表。用戶可以使用默認(rèn)樣式創(chuàng)建列表,也可以根據(jù)自己的需求自定義列表的外觀,例如,可以選擇不同的編號格式或項目符號樣式,以便更好地匹配文檔的整體設(shè)計和風(fēng)格。
接下來,我們將逐一介紹如何使用Python在Word中創(chuàng)建這些列表。
使用默認(rèn)樣式創(chuàng)建有序(編號)列表
有序列表,也稱為編號列表,它使用數(shù)字或字母標(biāo)識項目,主要用于按特定順序呈現(xiàn)步驟、排名或時間線等信息。
要在Word文檔中創(chuàng)建有序(編號)列表,只需使用 Section.AddParagraph() 方法添加段落,然后通過 Paragraph.ListFormat.ApplyNumberedStyle() 方法為這些段落應(yīng)用默認(rèn)的編號列表樣式。
以下是具體的實現(xiàn)步驟:
初始化Document類的實例來創(chuàng)建一個新的Word文檔。
使用Document.AddSection()方法向文檔添加一個節(jié)。
創(chuàng)建一個List,用于存放生成列表的項目。
遍歷List。
- 使用Section.AddParagraph()方法為當(dāng)前列表項添加一個段落。
- 使用Paragraph.AppendText()方法將當(dāng)前列表項的文本內(nèi)容添加到段落。
- 使用Paragraph.ListFormat.ApplyNumberedStyle()方法為段落應(yīng)用默認(rèn)的編號列表樣式。
使用Document.SaveToFile()方法保存文檔。
實現(xiàn)代碼
以下代碼展示了如何使用Python在Word文檔中使用默認(rèn)樣式創(chuàng)建有序(編號)列表:
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個節(jié)
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
para.Format.AfterSpacing = 5.0
text_range = para.AppendText("有序列表示例:")
text_range.CharacterFormat.FontName = "宋體"
# 創(chuàng)建項目列表
items = ["有序列表項目1", "有序列表項目2", "有序列表項目3"]
# 將每個項目作為編號列表添加到文檔中
for item in items:
# 為每個項目添加一個段落
para = section.AddParagraph()
text_range = para.AppendText(item)
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 保存文檔
doc.SaveToFile("默認(rèn)有序列表.docx", FileFormat.Docx2016)
doc.Close()
使用默認(rèn)樣式創(chuàng)建無序(項目符號)列表
無序列表,也叫做項目符號列表,它使用符號(如圓點(diǎn)或方塊)標(biāo)識項目,強(qiáng)調(diào)項目之間的關(guān)聯(lián)性而不關(guān)心順序,適用于列出相關(guān)特性或要點(diǎn)。
要在Word文檔中創(chuàng)建無序(項目符號)列表,可以使用Section.AddParagraph()和Paragraph.ListFormat.ApplyBulletStyle()方法。具體的實現(xiàn)步驟與創(chuàng)建有序(編號)列表基本相同,此處不再詳細(xì)說明。
實現(xiàn)代碼
以下代碼展示了如何使用Python在Word文檔中使用默認(rèn)樣式創(chuàng)建無序(項目符號)列表:
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個節(jié)
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
para.Format.AfterSpacing = 5.0
text_range = para.AppendText("無序列表示例:")
text_range.CharacterFormat.FontName = "宋體"
# 創(chuàng)建項目列表
items = ["無序列表項目1", "無序列表項目2", "無序列表項目3"]
# 將每個項目作為項目符號列表添加到文檔中
for item in items:
# 為每個項目添加一個段落
para = section.AddParagraph()
text_range = para.AppendText(item)
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)項目符號列表樣式應(yīng)用于段落
para.ListFormat.ApplyBulletStyle()
# 保存文檔
doc.SaveToFile("默認(rèn)無序列表.docx", FileFormat.Docx2016)
doc.Close()
創(chuàng)建多級列表
多級列表是一種包含多個層級的列表結(jié)構(gòu),允許在同一列表中結(jié)合有序和無序項目,以展示信息的層次關(guān)系和分類,使內(nèi)容更加清晰和有條理。適用于創(chuàng)建大綱、分類數(shù)據(jù)或構(gòu)建具有多個縮進(jìn)級別的詳細(xì)列表。
Spire.Doc主要提供了兩種方法來管理多級列表的級別:
1.直接設(shè)置列表級別
使用Paragraph.ListFormat.ListLevelNumber屬性直接定義每個段落的列表級別。該屬性接受從0到8的值,其中0表示級別1,8表示級別9。
2.調(diào)整列表項目的縮進(jìn)級別
或者使用Paragraph.ListFormat.IncreaseIndentLevel()或Paragraph.ListFormat.DecreaseIndentLevel()方法增加或減少列表項目的縮進(jìn)級別。
實現(xiàn)代碼
方法1:通過直接設(shè)置每個段落的列表級別來創(chuàng)建多級列表
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個節(jié)
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
para.Format.AfterSpacing = 5.0
text_range = para.AppendText("多級列表示例:")
text_range.CharacterFormat.FontName = "宋體"
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別1")
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 設(shè)置列表級別為級別1
para.ListFormat.ListLevelNumber = 0
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別2")
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 設(shè)置列表級別為級別2
para.ListFormat.ListLevelNumber = 1
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別1")
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 設(shè)置列表級別為級別1
para.ListFormat.ListLevelNumber = 0
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別2")
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 設(shè)置列表級別為級別2
para.ListFormat.ListLevelNumber = 1
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別3")
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 設(shè)置列表級別為級別3
para.ListFormat.ListLevelNumber = 2
# 保存文檔
doc.SaveToFile("多級列表.docx", FileFormat.Docx2016)
doc.Close()
方法2:通過增加或減少列表項目的縮進(jìn)級別來創(chuàng)建多級列表:
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個節(jié)
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
para.Format.AfterSpacing = 5.0
text_range = para.AppendText("多級列表示例:")
text_range.CharacterFormat.FontName = "宋體"
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別1")
text_range.CharacterFormat.FontName = "宋體"
# 將默認(rèn)編號列表樣式應(yīng)用于段落
para.ListFormat.ApplyNumberedStyle()
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別2")
text_range.CharacterFormat.FontName = "宋體"
# 增加縮進(jìn)級別
para.ListFormat.IncreaseIndentLevel()
# 繼續(xù)上一個列表
para.ListFormat.ContinueListNumbering()
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別1")
text_range.CharacterFormat.FontName = "宋體"
# 減少縮進(jìn)級別
para.ListFormat.DecreaseIndentLevel()
# 繼續(xù)上一個列表
para.ListFormat.ContinueListNumbering()
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別2")
text_range.CharacterFormat.FontName = "宋體"
# 增加縮進(jìn)級別
para.ListFormat.IncreaseIndentLevel()
# 繼續(xù)上一個列表
para.ListFormat.ContinueListNumbering()
# 添加段落
para = section.AddParagraph()
text_range = para.AppendText("多級列表 - 級別3")
text_range.CharacterFormat.FontName = "宋體"
# 增加縮進(jìn)級別
para.ListFormat.IncreaseIndentLevel()
# 繼續(xù)上一個列表
para.ListFormat.ContinueListNumbering()
# 保存文檔
doc.SaveToFile("多級列表.docx", FileFormat.Docx2016)
doc.Close()
使用自定義樣式創(chuàng)建列表
除了默認(rèn)列表樣式外,還可以給列表應(yīng)用自定義樣式。
要實現(xiàn)此功能,首先需要使用ListStyle類創(chuàng)建自定義列表樣式,并使用該類的屬性自定義其外觀。然后使用Document.ListStyles.Add()方法將該列表樣式添加到文檔中。最后,使用Paragraph.ListFormat.ApplyStyle()方法將該列表樣式應(yīng)用于段落。
實現(xiàn)代碼
以下代碼展示了如何使用Python在Word文檔中創(chuàng)建自定義樣式的編號列表:
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個部分
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
text_range = para.AppendText("自定義編號列表示例:")
# 設(shè)置標(biāo)題字體
text_range.CharacterFormat.FontName = "宋體"
para.Format.AfterSpacing = 5.0
# 創(chuàng)建自定義編號列表樣式,指定第一級的編號前綴、后綴和編號類型
listStyle = ListStyle(doc, ListType.Numbered)
listStyle.Name = "自定義樣式"
listStyle.Levels[0].NumberPrefix = "("
listStyle.Levels[0].NumberSufix = ")"
listStyle.Levels[0].PatternType = ListPatternType.Arabic
# 將列表樣式添加到文檔
doc.ListStyles.Add(listStyle)
# 添加段落并應(yīng)用自定義列表樣式
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目1")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目2")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目3")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目4")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
# 保存文檔
doc.SaveToFile("自定義編號列表.docx", FileFormat.Docx2016)
doc.Dispose()
以下代碼展示了如何在Python中創(chuàng)建自定義樣式的項目符號列表:
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個節(jié)
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
para.Format.AfterSpacing = 5.0
text_range = para.AppendText("自定義項目符號列表示例:")
# 設(shè)置標(biāo)題字體
text_range.CharacterFormat.FontName = "宋體"
# 創(chuàng)建自定義項目符號列表樣式,指定第一級的項目符號字符和字體
listStyle = ListStyle(doc, ListType.Bulleted)
listStyle.Name = "自定義樣式"
listStyle.Levels[0].BulletCharacter = "\u002A"
listStyle.Levels[0].CharacterFormat.FontName = "Symbol"
# 將列表樣式添加到文檔
doc.ListStyles.Add(listStyle)
# 添加段落并應(yīng)用自定義列表樣式
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目1")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目2")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表項目3")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
# 保存文檔
doc.SaveToFile("自定義項目符號列表.docx", FileFormat.Docx2016)
doc.Dispose()
以下代碼展示了如何在Python中創(chuàng)建帶前一級編號前綴的多級編號列表:
from spire.doc import *
# 創(chuàng)建新的Word文檔
doc = Document()
# 向文檔添加一個節(jié)
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 添加一個標(biāo)題段落
para = section.AddParagraph()
para.Format.AfterSpacing = 5.0
text_range = para.AppendText("自定義多級列表示例:")
# 設(shè)置標(biāo)題字體
text_range.CharacterFormat.FontName = "宋體"
# 創(chuàng)建自定義編號列表樣式,指定特定級別的編號前綴和編號類型
listStyle = ListStyle(doc, ListType.Numbered)
listStyle.Name = "自定義樣式"
listStyle.Levels[0].PatternType = ListPatternType.Arabic
# NumberPrefix的值應(yīng)滿足語法"%n",以將上一級列表值更新為當(dāng)前級別的前綴
listStyle.Levels[1].NumberPrefix = "%1."
listStyle.Levels[1].PatternType = ListPatternType.Arabic
listStyle.Levels[2].NumberPrefix = "%1.%2."
listStyle.Levels[2].PatternType = ListPatternType.Arabic
# 將列表樣式添加到文檔
doc.ListStyles.Add(listStyle)
# 添加段落并應(yīng)用自定義列表樣式
para = section.AddParagraph()
text_range = para.AppendText("自定義列表 - 級別 1")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表 - 級別 1")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
para = section.AddParagraph()
text_range = para.AppendText("自定義列表 - 級別 2")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 1
para = section.AddParagraph()
text_range = para.AppendText("自定義列表 - 級別 2")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ContinueListNumbering()
para.ListFormat.ApplyStyle("自定義樣式")
para = section.AddParagraph()
text_range = para.AppendText("自定義列表 - 級別 3")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 2
para = section.AddParagraph()
text_range = para.AppendText("自定義列表 - 級別 1")
text_range.CharacterFormat.FontName = "宋體"
para.ListFormat.ApplyStyle("自定義樣式")
para.ListFormat.ListLevelNumber = 0
# 保存文檔
doc.SaveToFile("自定義多級編號列表.docx", FileFormat.Docx2016)
doc.Dispose()
Python讀取Word中的列表
要從Word文檔中獲取列表,你需要遍歷段落并判斷它們是否應(yīng)用了列表格式。對于具有列表格式的段落,你可以通過以下屬性獲取列表的詳細(xì)信息,例如列表編號或項目符號、列表項的文本內(nèi)容、列表類型和嵌套級別:
- Paragraph.ListText:獲取列表編號或項目符號。
- Paragraph.Text:獲取列表項的文本內(nèi)容。
- Paragraph.ListFormat.ListType:獲取列表的類型(例如,編號、項目符號)。
- Paragraph.ListFormat.ListLevelNumber:獲取列表項的嵌套級別(從0開始)。
實現(xiàn)代碼
以下代碼展示了如何使用Python從Word文檔中獲取列表:
from spire.doc import *
# 打開現(xiàn)有的Word文檔
doc = Document()
doc.LoadFromFile("多級列表.docx")
# 獲取第一個節(jié)
section = doc.Sections[0]
# 遍歷節(jié)中的段落
for para_index in range(section.Paragraphs.Count):
para = section.Paragraphs[para_index]
# 查找具有列表格式的段落
if(para.ListFormat.ListType != ListType.NoList):
# 提取列表編號或項目符號、列表項的文本內(nèi)容、列表類型和嵌套級別
print(f"列表編號和內(nèi)容: {para.ListText + para.Text}")
print(f"列表類型: {para.ListFormat.ListType}")
print(f"列表級別: {para.ListFormat.ListLevelNumber + 1}")
doc.Close()
Python從Word中刪除列表
如果你需要清除Word段落的列表格式,只需調(diào)用Paragraph.ListFormat.RemoveList()方法。該方法將從段落中刪除項目符號或編號,同時保留文本。
實現(xiàn)代碼
以下代碼展示了如何使用Python清除Word段落的列表格式:
from spire.doc import *
# 打開現(xiàn)有的Word文檔
doc = Document()
doc.LoadFromFile("多級列表.docx")
# 獲取第一個節(jié)
section = doc.Sections[0]
# 從段落中刪除列表格式
for para_index in range(section.Paragraphs.Count):
para = section.Paragraphs[para_index]
if(para.ListFormat.ListType != ListType.NoList):
para.ListFormat.RemoveList()
# 保存文檔
doc.SaveToFile("刪除列表.docx", FileFormat.Docx2016)
doc.Dispose()
到此這篇關(guān)于Python實現(xiàn)在Word中創(chuàng)建,讀取和刪除列表詳解的文章就介紹到這了,更多相關(guān)Python Word列表操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python enumerate內(nèi)置函數(shù)用法總結(jié)
這篇文章主要介紹了python enumerate內(nèi)置函數(shù)用法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Python matplotlib通過plt.scatter畫空心圓標(biāo)記出特定的點(diǎn)方法
今天小編就為大家分享一篇Python matplotlib通過plt.scatter畫空心圓標(biāo)記出特定的點(diǎn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
jupyter note 實現(xiàn)將數(shù)據(jù)保存為word
這篇文章主要介紹了jupyter note 實現(xiàn)將數(shù)據(jù)保存為word,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python+selenium+chrome批量文件下載并自動創(chuàng)建文件夾實例
這篇文章主要介紹了python+selenium+chrome批量文件下載并自動創(chuàng)建文件夾實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python Flask基礎(chǔ)到登錄功能的實現(xiàn)代碼
這篇文章主要介紹了Python Flask基礎(chǔ)到登錄功能的實現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05
pandas把所有大于0的數(shù)設(shè)置為1的方法
今天小編就為大家分享一篇pandas把所有大于0的數(shù)設(shè)置為1的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python通過kerberos安全認(rèn)證操作kafka方式
這篇文章主要介紹了Python通過kerberos安全認(rèn)證操作kafka方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

