最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2025年01月23日 15:59:01   作者:Eiceblue  
在Word文檔中,超鏈接是一種將文本或圖像連接到其他文檔、網(wǎng)頁(yè)或同一文檔中不同部分的功能,本文將為大家介紹一下Python如何實(shí)現(xiàn)在Word中添加或刪除超鏈接,需要的可以參考下

在Word文檔中,超鏈接是一種將文本或圖像連接到其他文檔、網(wǎng)頁(yè)或同一文檔中不同部分的功能。通過(guò)添加超鏈接,用戶可以輕松地導(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 – 代表超鏈接類型 (包括網(wǎng)頁(yè)鏈接WebLink、郵件鏈接EMailLink、書簽鏈接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)建書簽
section2 = doc.AddSection()
bookmarkParagrapg = section2.AddParagraph()
bookmarkParagrapg.AppendText("添加一個(gè)新段落")
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)

# 添加一個(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中的 enum 模塊源碼詳析

    Python中的 enum 模塊源碼詳析

    這篇文章主要給大家介紹了關(guān)于Python中 enum 模塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Python中縮進(jìn)錯(cuò)誤的原因及解決

    Python中縮進(jìn)錯(cuò)誤的原因及解決

    縮進(jìn)錯(cuò)誤是Python中最常見(jiàn)的錯(cuò)誤之一,它會(huì)使我們的代碼難以理解,并且難以調(diào)試,下面就來(lái)介紹一下,具有一定的參考價(jià)值,感興趣的額可以了解一下
    2025-05-05
  • python subprocess 殺掉全部派生的子進(jìn)程方法

    python subprocess 殺掉全部派生的子進(jìn)程方法

    下面小編就為大家?guī)?lái)一篇python subprocess 殺掉全部派生的子進(jìn)程方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • 用Python實(shí)現(xiàn)給Word文檔蓋章

    用Python實(shí)現(xiàn)給Word文檔蓋章

    大家好,本篇文章主要講的是用Python實(shí)現(xiàn)給Word文檔蓋章,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • Python中使用ElementTree解析XML示例

    Python中使用ElementTree解析XML示例

    這篇文章主要介紹了Python中使用ElementTree解析XML示例,本文同時(shí)講解了XML基本概念介紹、XML幾種解析方法和ElementTree解析實(shí)例,需要的朋友可以參考下
    2015-06-06
  • python logging模塊的使用詳解

    python logging模塊的使用詳解

    這篇文章主要介紹了python logging模塊的使用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-10-10
  • python圖像處理基本操作總結(jié)(PIL庫(kù)、Matplotlib及Numpy)

    python圖像處理基本操作總結(jié)(PIL庫(kù)、Matplotlib及Numpy)

    這篇文章主要給大家介紹了關(guān)于python圖像處理基本操作的相關(guān)資料,主要利用的是PIL庫(kù)、Matplotlib及Numpy等處理方法,需要的朋友可以參考下
    2021-06-06
  • Django單元測(cè)試工具test client使用詳解

    Django單元測(cè)試工具test client使用詳解

    這篇文章主要介紹了Django單元測(cè)試工具test client使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python基礎(chǔ)之Spyder的使用

    Python基礎(chǔ)之Spyder的使用

    Spyder是一個(gè)用于科學(xué)計(jì)算的使用Python編程語(yǔ)言的集成開(kāi)發(fā)環(huán)境(IDE),它結(jié)合了綜合開(kāi)發(fā)工具的高級(jí)編輯、分析、調(diào)試等功能,需要的朋友可以參考下
    2023-05-05
  • python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格

    python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評(píng)論

临桂县| 宁津县| 理塘县| 普宁市| 文水县| 大余县| 龙江县| 侯马市| 秭归县| 勃利县| 咸阳市| 同江市| 裕民县| 台前县| 固镇县| 兴义市| 镇平县| 胶南市| 那曲县| 红桥区| 芦山县| 巨野县| 仁寿县| 山西省| 金秀| 兴城市| 南宁市| 景德镇市| 琼中| 灵丘县| 建阳市| 宝兴县| 保定市| 依安县| 台湾省| 东辽县| 永清县| 巩留县| 建水县| 泽州县| 镇赉县|