使用Python實(shí)現(xiàn)在Word中添加或刪除超鏈接
在Word文檔中,超鏈接是一種將文本或圖像連接到其他文檔、網(wǎng)頁(yè)或同一文檔中不同部分的功能。通過(guò)添加超鏈接,用戶(hù)可以輕松地導(dǎo)航到相關(guān)信息,從而增強(qiáng)文檔的互動(dòng)性和可讀性。本文將介紹如何使用Python在Word中添加超鏈接、或刪除Word文檔中的超鏈接。
要實(shí)現(xiàn)通過(guò)Python操作Word文檔,我們需要安裝 Spire.Doc for Python 庫(kù)。該庫(kù)的pip安裝命令如下:
pip install Spire.Doc
Python 在Word中添加超鏈接
Spire.Doc for Python 庫(kù)提供了 AppendHyperlink() 方法來(lái)添加超鏈接,其中三個(gè)參數(shù):
• link – 代表超鏈接地址
• text – 代表顯示文本 (也可傳入picture來(lái)為圖片添加超鏈接)
• type – 代表超鏈接類(lèi)型 (包括網(wǎng)頁(yè)鏈接WebLink、郵件鏈接EMailLink、書(shū)簽鏈接Bookmark、文件鏈接FileLink)
示例代碼如下:
from spire.doc import * from spire.doc.common import * # 創(chuàng)建Word文檔 doc = Document() # 添加一節(jié) section = doc.AddSection() # 添加一個(gè)段落 paragraph = section.AddParagraph() # 添加一個(gè)簡(jiǎn)單網(wǎng)頁(yè)鏈接 paragraph.AppendHyperlink("https://ABCD.com/", "主頁(yè)", HyperlinkType.WebLink) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個(gè)郵箱鏈接 paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "郵箱地址", HyperlinkType.EMailLink) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個(gè)文檔鏈接 filePath = "C:\\Users\\Administrator\\Desktop\\排名.xlsx" paragraph.AppendHyperlink(filePath, "點(diǎn)擊查看文件", HyperlinkType.FileLink) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個(gè)新節(jié)并創(chuàng)建書(shū)簽 section2 = doc.AddSection() bookmarkParagrapg = section2.AddParagraph() bookmarkParagrapg.AppendText("添加一個(gè)新段落") start = bookmarkParagrapg.AppendBookmarkStart("書(shū)簽") bookmarkParagrapg.Items.Insert(0, start) bookmarkParagrapg.AppendBookmarkEnd("書(shū)簽") # 鏈接到書(shū)簽 paragraph.AppendHyperlink("書(shū)簽", "點(diǎn)擊跳轉(zhuǎn)到文檔指定位置", HyperlinkType.Bookmark) # 添加換行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # 添加一個(gè)圖片超鏈接 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中的超鏈接
要?jiǎng)h除 Word 文檔中的所有超鏈接,先用到了自定義方法 FindAllHyperlinks()
來(lái)查找文檔中的所有超鏈接,然后再通過(guò)自定義方法 FlattenHyperlinks()
來(lái)扁平化超鏈接。
示例代碼如下:
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實(shí)現(xiàn)在Word中添加或刪除超鏈接的文章就介紹到這了,更多相關(guān)Python Word添加或刪除超鏈接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(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)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01利用Python實(shí)現(xiàn)生成并識(shí)別圖片驗(yàn)證碼
這篇文章主要為大家的詳細(xì)介紹了如何利用Python實(shí)現(xiàn)生成并識(shí)別圖片驗(yàn)證碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02如何使用?Python?實(shí)現(xiàn)?DeepSeek?R1?本地化部署
文章介紹了如何使用Python實(shí)現(xiàn)DeepSeekR1本地化部署,包括硬件環(huán)境、Python環(huán)境、安裝依賴(lài)包、配置與運(yùn)行代碼等步驟,幫助讀者輕松部署并運(yùn)行本地AI助手,感興趣的朋友一起看看吧2025-02-02如何在Python中自定義異常類(lèi)與異常處理機(jī)制
在Python編程中,異常處理是一種重要的編程范式,它允許我們?cè)诔绦蜻\(yùn)行時(shí)檢測(cè)并處理錯(cuò)誤,本文將介紹如何在Python中編寫(xiě)自定義的異常類(lèi),并詳細(xì)解釋Python的異常處理機(jī)制,感興趣的朋友一起看看吧2024-06-06使用Python將PDF表格提取到文本,CSV和Excel文件中
本文將介紹如何使用簡(jiǎn)單的Python代碼從PDF文檔中提取表格數(shù)據(jù)并將其寫(xiě)入文本、CSV和Excel文件,從而輕松實(shí)現(xiàn)PDF表格的自動(dòng)化提取,有需要的可以參考下2024-11-11