使用Python實現(xiàn)在Word中添加或刪除超鏈接
在Word文檔中,超鏈接是一種將文本或圖像連接到其他文檔、網(wǎng)頁或同一文檔中不同部分的功能。通過添加超鏈接,用戶可以輕松地導(dǎo)航到相關(guān)信息,從而增強(qiáng)文檔的互動性和可讀性。本文將介紹如何使用Python在Word中添加超鏈接、或刪除Word文檔中的超鏈接。
要實現(xiàn)通過Python操作Word文檔,我們需要安裝 Spire.Doc for Python 庫。該庫的pip安裝命令如下:
pip install Spire.Doc
Python 在Word中添加超鏈接
Spire.Doc for Python 庫提供了 AppendHyperlink() 方法來添加超鏈接,其中三個參數(shù):
• link – 代表超鏈接地址
• text – 代表顯示文本 (也可傳入picture來為圖片添加超鏈接)
• type – 代表超鏈接類型 (包括網(wǎng)頁鏈接WebLink、郵件鏈接EMailLink、書簽鏈接Bookmark、文件鏈接FileLink)
示例代碼如下:
from spire.doc import * from spire.doc.common import * # 創(chuàng)建Word文檔 doc = Document() # 添加一節(jié) section = doc.AddSection() # 添加一個段落 paragraph = section.AddParagraph() # 添加一個簡單網(wǎng)頁鏈接 paragraph.AppendHyperlink("https://ABCD.com/", "主頁", HyperlinkType.WebLink) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個郵箱鏈接 paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "郵箱地址", HyperlinkType.EMailLink) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個文檔鏈接 filePath = "C:\\Users\\Administrator\\Desktop\\排名.xlsx" paragraph.AppendHyperlink(filePath, "點(diǎn)擊查看文件", HyperlinkType.FileLink) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個新節(jié)并創(chuàng)建書簽 section2 = doc.AddSection() bookmarkParagrapg = section2.AddParagraph() bookmarkParagrapg.AppendText("添加一個新段落") start = bookmarkParagrapg.AppendBookmarkStart("書簽") bookmarkParagrapg.Items.Insert(0, start) bookmarkParagrapg.AppendBookmarkEnd("書簽") # 鏈接到書簽 paragraph.AppendHyperlink("書簽", "點(diǎn)擊跳轉(zhuǎn)到文檔指定位置", HyperlinkType.Bookmark) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個圖片超鏈接 image = "C:\\Users\\Administrator\\Desktop\\work1.jpg" picture = paragraph.AppendPicture(image) paragraph.AppendHyperlink("https://ABCD.com/", picture, HyperlinkType.WebLink) # 保存文檔 doc.SaveToFile("Word超鏈接.docx", FileFormat.Docx2019); doc.Dispose()
生成文檔:
Python 刪除Word中的超鏈接
要刪除 Word 文檔中的所有超鏈接,先用到了自定義方法 FindAllHyperlinks()
來查找文檔中的所有超鏈接,然后再通過自定義方法 FlattenHyperlinks()
來扁平化超鏈接。
示例代碼如下:
from spire.doc import * from spire.doc.common import * # 查找文檔中的所有超鏈接 def FindAllHyperlinks(document): hyperlinks = [] for i in range(document.Sections.Count): section = document.Sections.get_Item(i) for j in range(section.Body.ChildObjects.Count): sec = section.Body.ChildObjects.get_Item(j) if sec.DocumentObjectType == DocumentObjectType.Paragraph: for k in range((sec if isinstance(sec, Paragraph) else None).ChildObjects.Count): para = (sec if isinstance(sec, Paragraph) else None).ChildObjects.get_Item(k) if para.DocumentObjectType == DocumentObjectType.Field: field = para if isinstance(para, Field) else None if field.Type == FieldType.FieldHyperlink: hyperlinks.append(field) return hyperlinks # 扁平化超鏈接域 def FlattenHyperlinks(field): ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf( field.OwnerParagraph) fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field) sepOwnerPara = field.Separator.OwnerParagraph sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf( field.Separator.OwnerParagraph) sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf( field.Separator) endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End) endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf( field.End.OwnerParagraph) FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex) field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex) for i in range(sepOwnerParaIndex, ownerParaIndex - 1, -1): if i == sepOwnerParaIndex and i == ownerParaIndex: for j in range(sepIndex, fieldIndex - 1, -1): field.OwnerParagraph.ChildObjects.RemoveAt(j) elif i == ownerParaIndex: for j in range(field.OwnerParagraph.ChildObjects.Count - 1, fieldIndex - 1, -1): field.OwnerParagraph.ChildObjects.RemoveAt(j) elif i == sepOwnerParaIndex: for j in range(sepIndex, -1, -1): sepOwnerPara.ChildObjects.RemoveAt(j) else: field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i) # 將域轉(zhuǎn)換為文本范圍并清除文本格式 def FormatFieldResultText(ownerBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex): for i in range(sepOwnerParaIndex, endOwnerParaIndex + 1): para = ownerBody.ChildObjects[i] if isinstance( ownerBody.ChildObjects[i], Paragraph) else None if i == sepOwnerParaIndex and i == endOwnerParaIndex: for j in range(sepIndex + 1, endIndex): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) elif i == sepOwnerParaIndex: for j in range(sepIndex + 1, para.ChildObjects.Count): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) elif i == endOwnerParaIndex: for j in range(0, endIndex): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) else: for j, unusedItem in enumerate(para.ChildObjects): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) # 設(shè)置文本樣式 def FormatText(tr): tr.CharacterFormat.TextColor = Color.get_Black() tr.CharacterFormat.UnderlineStyle = UnderlineStyle.none # 加載Word文檔 doc = Document() doc.LoadFromFile("Word超鏈接.docx") # 獲取所有超鏈接 hyperlinks = FindAllHyperlinks(doc) # 扁平化超鏈接 for i in range(len(hyperlinks) - 1, -1, -1): FlattenHyperlinks(hyperlinks[i]) # 保存文件 doc.SaveToFile("刪除超鏈接.docx", FileFormat.Docx) doc.Close()
生成文件:
到此這篇關(guān)于使用Python實現(xiàn)在Word中添加或刪除超鏈接的文章就介紹到這了,更多相關(guān)Python Word添加或刪除超鏈接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python調(diào)用系統(tǒng)底層API播放wav文件的方法
這篇文章主要介紹了Python調(diào)用系統(tǒng)底層API播放wav文件的方法,涉及Python使用pywin32調(diào)用系統(tǒng)底層API讀取與播放wav文件的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08django文檔學(xué)習(xí)之a(chǎn)pplications使用詳解
這篇文章主要介紹了Python文檔學(xué)習(xí)之a(chǎn)pplications使用詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01如何使用?Python?實現(xiàn)?DeepSeek?R1?本地化部署
文章介紹了如何使用Python實現(xiàn)DeepSeekR1本地化部署,包括硬件環(huán)境、Python環(huán)境、安裝依賴包、配置與運(yùn)行代碼等步驟,幫助讀者輕松部署并運(yùn)行本地AI助手,感興趣的朋友一起看看吧2025-02-02使用Python將PDF表格提取到文本,CSV和Excel文件中
本文將介紹如何使用簡單的Python代碼從PDF文檔中提取表格數(shù)據(jù)并將其寫入文本、CSV和Excel文件,從而輕松實現(xiàn)PDF表格的自動化提取,有需要的可以參考下2024-11-11