You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:如何从.ods文件中读取超链接?

从ODS文件中提取超链接的解决方案

方案:使用odfpy库解析ODS文件

odfpy是专门处理OpenDocument格式(包括.ods)的Python库,能直接解析文件底层结构,从而提取单元格中的超链接信息。

步骤1:安装odfpy

pip install odfpy

步骤2:完整提取数据与超链接

下面的函数会读取ODS文件,同时提取单元格的显示文本和对应的超链接,最终返回包含原始数据和超链接列的DataFrame:

from odf.opendocument import load
from odf.table import Table, TableRow, TableCell
from odf.text import P, A
import pandas as pd

def extract_ods_with_hyperlinks(file_path, sheet_index=0):
    # 加载ODS文档
    doc = load(file_path)
    # 获取指定索引的工作表(默认第一个)
    target_sheet = doc.getElementsByType(Table)[sheet_index]
    
    all_rows_data = []
    # 遍历工作表中的每一行
    for row in target_sheet.getElementsByType(TableRow):
        cell_entries = []
        # 遍历行中的每个单元格
        for cell in row.getElementsByType(TableCell):
            cell_text = ""
            cell_link = None
            # 解析单元格内的段落元素
            for paragraph in cell.getElementsByType(P):
                # 检查是否存在超链接标签(A标签)
                for link in paragraph.getElementsByType(A):
                    cell_link = link.getAttribute('xlink:href')
                    cell_text = paragraph.getText()
                    break
                # 如果没有超链接,直接取单元格文本
                if not cell_link:
                    cell_text = paragraph.getText()
            cell_entries.append({"content": cell_text, "hyperlink": cell_link})
        if cell_entries:
            all_rows_data.append(cell_entries)
    
    # 转换为DataFrame格式
    # 提取表头
    headers = [entry["content"] for entry in all_rows_data[0]]
    # 构建数据行
    data_rows = []
    for row in all_rows_data[1:]:
        row_dict = {}
        for idx, entry in enumerate(row):
            row_dict[headers[idx]] = entry["content"]
            # 添加对应的超链接列,命名为「原列名_hyperlink」
            row_dict[f"{headers[idx]}_hyperlink"] = entry["hyperlink"]
        data_rows.append(row_dict)
    
    return pd.DataFrame(data_rows)

# 使用示例
result_df = extract_ods_with_hyperlinks("你的文件路径.ods")
print(result_df)

方案2:结合Pandas现有结果补充超链接

如果你已经习惯用pd.read_excel()读取数据,可以用下面的方法单独提取超链接,再合并到已有的DataFrame中:

def append_hyperlinks_to_df(file_path, existing_df, sheet_index=0):
    doc = load(file_path)
    target_sheet = doc.getElementsByType(Table)[sheet_index]
    column_names = existing_df.columns.tolist()
    
    # 为每一列添加对应的超链接列
    for col_idx, col_name in enumerate(column_names):
        link_col = f"{col_name}_hyperlink"
        existing_df[link_col] = None
        
        row_num = 0
        # 跳过表头行,遍历数据行
        for row in target_sheet.getElementsByType(TableRow)[1:]:
            if row_num >= len(existing_df):
                break
            cells = row.getElementsByType(TableCell)
            if col_idx >= len(cells):
                row_num += 1
                continue
            target_cell = cells[col_idx]
            link = None
            # 查找单元格内的超链接
            for paragraph in target_cell.getElementsByType(P):
                for a_tag in paragraph.getElementsByType(A):
                    link = a_tag.getAttribute('xlink:href')
                    break
            existing_df.at[row_num, link_col] = link
            row_num += 1
    return existing_df

# 使用示例
original_df = pd.read_excel("你的文件路径.ods")
df_with_links = append_hyperlinks_to_df("你的文件路径.ods", original_df)
print(df_with_links)

说明

  • odfpy会直接解析ODS的XML结构,能准确获取到单元格中的超链接属性(xlink:href)
  • 两种方案都能保留原始数据的同时,把超链接作为单独列存储,方便后续处理

内容的提问来源于stack exchange,提问作者Lewis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 09:51:04