当前位置: 首页 > news >正文

网站推广论坛企业网站建设用语

网站推广论坛,企业网站建设用语,长沙有哪个学校可以学网站建设,可以做app的软件在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超链接,用户可以轻松地导航到相关信息,从而增强文档的互动性和可读性。本文将介绍如何使用Python在Word中添加超链接、或删除Word文档中的超…

在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超链接,用户可以轻松地导航到相关信息,从而增强文档的互动性和可读性。本文将介绍如何使用Python在Word中添加超链接、或删除Word文档中的超链接

文章目录

    • Python 在Word中添加超链接
    • Python 删除Word中的超链接

要实现通过Python操作Word文档,我们需要安装 Spire.Doc for Python 库。该库的pip安装命令如下:

pip install Spire.Doc

Python 在Word中添加超链接

Spire.Doc for Python 库提供了 AppendHyperlink() 方法来添加超链接,其中三个参数:
link – 代表超链接地址
text – 代表显示文本 (也可传入picture来为图片添加超链接)
type – 代表超链接类型 (包括网页链接WebLink、邮件链接EMailLink、书签链接Bookmark、文件链接FileLink

示例代码如下:

from spire.doc import *
from spire.doc.common import *# 创建Word文档
doc = Document()# 添加一节
section = doc.AddSection()# 添加一个段落
paragraph = section.AddParagraph()# 添加一个简单网页链接
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, "点击查看文件", HyperlinkType.FileLink)# 添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)# 添加一个新节并创建书签
section2 = doc.AddSection()
bookmarkParagrapg = section2.AddParagraph()
bookmarkParagrapg.AppendText("添加一个新段落")
start = bookmarkParagrapg.AppendBookmarkStart("书签")
bookmarkParagrapg.Items.Insert(0, start)
bookmarkParagrapg.AppendBookmarkEnd("书签")# 链接到书签
paragraph.AppendHyperlink("书签", "点击跳转到文档指定位置", 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()

生成文档:
Word超链接

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 Noneif 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.OwnerParagraphsepOwnerParaIndex = 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)# 将域转换为文本范围并清除文本格式
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 Noneif 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])# 设置文本样式
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()

生成文件:
删除超链接


如何去除水印?点击申请一个月试用授权:
https://www.e-iceblue.com/TemLicense.html

http://www.yayakq.cn/news/413014/

相关文章:

  • 网站管理建设需进一步加强网站建设上海哪家公司好
  • 网络推广方案怎么写文库网站怎么做seo
  • 做游戏模型素材 网站做app还是做微网站好
  • 镇江市建设工程招投标网站办公室设计报价
  • 公司想建立一个网站吗教育机构退费法律规定
  • 网站开发合同是否是技术合同巨量算数数据分析
  • 网站做代码图像显示不出来的租号网站咋做
  • 如何制作产品网站模板下载地址建设网站需要学习什么语言
  • 信阳住房和城乡建设厅网站wordpress 淡入特效
  • 网上商城网站设计那个网站教你做美食
  • 大型网站开发团队网站制作教程视频
  • 网站开发页面静态化技术网站建设氺金手指排名12
  • 凯里网站制作盛泽做网站的
  • 网站开发公对公转账合同模板网页制作学什么最好
  • 国外做各种趣味实验的网站wordpress鼠标指针
  • 营销型网站应用做移动端网站软件
  • 聊城建设学校地址seo专员工作容易学吗
  • 全flash网站欣赏phpstudy搭建本地网站
  • 装修之家网站wordpress生成推广链接
  • 深圳住房和建设局网站网站源码怎么做
  • 婚介网站开发苏州房产网
  • 杭州余杭网站建设大连58同城招聘网最新招聘
  • 受欢迎的集团网站建设东莞集团网站建设
  • 建网站的公司有没有免费网站制作
  • 我是做化工回收的做哪个网站比较好几个月网站没有排名
  • 网站后台模板psd网站域名怎么快速备案价格
  • 打造对外宣传工作平台网站建设云服务器租用价格表
  • 沈阳做网站培训厂家网页制作
  • 做视频网站赚钱吗广告设计与制作专业描述
  • 襄阳网站建设公司深度网