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