python - python - 如何提取 DOCX 超链接的文本?
问题描述
基于此解决方案:
from docx import Document
from docx.opc.constants import RELATIONSHIP_TYPE as RT
document = Document('test.docx')
rels = document.part.rels
def iter_hyperlink_rels(rels):
for rel in rels:
if rels[rel].reltype == RT.HYPERLINK:
yield rels[rel]._target
print(iter_hyperlink_rels(rels)
我需要获取超链接的 url和文本(例如mydomain.com
,用于 url 和Go to My Domain
文本)
解决方案
回答我自己的问题,我必须通过html
这样做:
from bs4 import BeautifulSoup
with open('my_word_file.htm', 'r') as file:
page = file.read()
soup = BeautifulSoup(page, 'lxml')
text_and_url = []
for link in soup.findAll('a'):
text_and_url.append({'text':link.string, 'url':link.get('href')})
docx
文件转换html
:
推荐阅读
- javascript - 如何返回 JavaScript XMLHttpRequest 响应?
- r - 选择有条件的前 N 个值
- html - 在不启用视频控件的情况下双击以全屏显示 (WordPress/HTML5)
- android - 如何为所有不同的屏幕尺寸自动对齐按钮和文本视图?
- r - 在两个数据框中按最近的 POSIXct 匹配值,同时保持两个日期
- docker - 在 jenkins docker 映像中安装插件失败
- neo4j - py2neo MERGE 导致 ConstraintValidationFailed 并行化时节点已存在
- asp.net - 将 Blazor 集成到 .NET Core ASP.NET MVC 应用程序中
- android - 垂直列表,如 android tv 中的 android 手机
- android - RecyclerView 显示 3 个项目,但仅显示我的数据库的最后一个条目