欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python實現(xiàn)在Word中添加或刪除超鏈接

 更新時間:2025年01月23日 15:59:01   作者:Eiceblue  
在Word文檔中,超鏈接是一種將文本或圖像連接到其他文檔、網(wǎng)頁或同一文檔中不同部分的功能,本文將為大家介紹一下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中win32com模塊的使用

    Python中win32com模塊的使用

    本文主要介紹了Python中win32com模塊的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python調(diào)用系統(tǒng)底層API播放wav文件的方法

    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-08
  • django文檔學(xué)習(xí)之a(chǎn)pplications使用詳解

    django文檔學(xué)習(xí)之a(chǎn)pplications使用詳解

    這篇文章主要介紹了Python文檔學(xué)習(xí)之a(chǎn)pplications使用詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python并查集Disjoint?Set的具體使用

    Python并查集Disjoint?Set的具體使用

    本文主要介紹了Python并查集Disjoint?Set的具體使用,包括并查集的基本概念、實現(xiàn)方式、路徑壓縮和應(yīng)用場景,并使用代碼示例演示并查集的操作,感興趣的可以了解一下
    2024-01-01
  • Matplotlib繪圖基礎(chǔ)之刻度詳解

    Matplotlib繪圖基礎(chǔ)之刻度詳解

    Matplotlib中刻度是用于在繪圖中表示數(shù)據(jù)大小的工具,通常以整數(shù)或小數(shù)表示,具體取決于坐標(biāo)軸的類型和限制,下面就為大家介紹一下Matplotlib中刻度是具體設(shè)置與使用吧
    2023-07-07
  • 利用Python實現(xiàn)生成并識別圖片驗證碼

    利用Python實現(xiàn)生成并識別圖片驗證碼

    這篇文章主要為大家的詳細(xì)介紹了如何利用Python實現(xiàn)生成并識別圖片驗證碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • 如何使用?Python?實現(xiàn)?DeepSeek?R1?本地化部署

    如何使用?Python?實現(xiàn)?DeepSeek?R1?本地化部署

    文章介紹了如何使用Python實現(xiàn)DeepSeekR1本地化部署,包括硬件環(huán)境、Python環(huán)境、安裝依賴包、配置與運(yùn)行代碼等步驟,幫助讀者輕松部署并運(yùn)行本地AI助手,感興趣的朋友一起看看吧
    2025-02-02
  • 如何在Python中自定義異常類與異常處理機(jī)制

    如何在Python中自定義異常類與異常處理機(jī)制

    在Python編程中,異常處理是一種重要的編程范式,它允許我們在程序運(yùn)行時檢測并處理錯誤,本文將介紹如何在Python中編寫自定義的異常類,并詳細(xì)解釋Python的異常處理機(jī)制,感興趣的朋友一起看看吧
    2024-06-06
  • 使用Python將PDF表格提取到文本,CSV和Excel文件中

    使用Python將PDF表格提取到文本,CSV和Excel文件中

    本文將介紹如何使用簡單的Python代碼從PDF文檔中提取表格數(shù)據(jù)并將其寫入文本、CSV和Excel文件,從而輕松實現(xiàn)PDF表格的自動化提取,有需要的可以參考下
    2024-11-11
  • pandas merge報錯的解決方案

    pandas merge報錯的解決方案

    這篇文章主要介紹了pandas merge報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評論