利用python的docx模块提取word中的包含超级链接的文本,并替换他。 这个问题找了好久,总有在github上找到了解决方案
from docx.oxml.ns import qn
def find_hyperlink_indoc(doc):
'''
:param doc: doc file get by doc = Document('./xxxx.docx')
:return: a list of all hyperlink item in doc.
'''
xml_e = doc.element
hyperlink_list = xml_e.findall('.//' + qn("w:hyperlink"))
return hyperlink_list
def get_hyperlink_text(hyperlink_item):
text = hyperlink_item.findall('.//' + qn("w:t"))[0].text
return text
def set_hyperlink_text(hyperlink_item, text):
hyperlink_item.findall('.//' + qn("w:t"))[0].text = text
doc = Document('./test.docx')
hl_list = find_hyperlink_indoc(doc)
for item in hl_list:
print(get_hyperlink_text(item))
set_hyperlink_text(item, 'testtest')
doc.save('./test_out.docx')
[点击并拖拽以移动]
供大家参看
该博客分享了如何使用Python的docx模块查找并替换Word文档中的超链接文本。通过find_hyperlink_in_doc函数遍历文档中的超链接,get_hyperlink_text获取超链接文本,set_hyperlink_text进行替换,最后保存修改后的文档。
1548

被折叠的 条评论
为什么被折叠?



