【问题标题】:Python-docx : Hyperlink text from markdown in tablePython-docx:表格中降价的超链接文本
【发布时间】:2021-12-21 07:14:40
【问题描述】:

我希望在 DOCX 表的文本中插入超链接。目前我拥有的数据有markdown格式的超链接。

有没有办法将此作为超链接添加到我的 DOCx 表中?我知道这可以为段落*here 完成,但想知道它是否可以在表格中完成。

示例代码:

import pandas as pd
import docx

# Sample Data

df = pd.DataFrame([["Some [string](www.google.com) is the link to google",'IDnum1',"China"],
              ["This is string 2","IDnum3","Australia"],
              ["some string3","IDNum5","America"]], columns = ["customer string","ID number","Country"])

# Docx DF to table

# open an existing document
doc = docx.Document()

# add a table to the end and create a reference variable
# extra row is so we can add the header row
t = doc.add_table(df.shape[0]+1, df.shape[1])

# add the header rows.
for j in range(df.shape[-1]):
    t.cell(0,j).text = df.columns[j]

# add the rest of the data frame
for i in range(df.shape[0]):
    for j in range(df.shape[-1]):
        t.cell(i+1,j).text = str(df.values[i,j])

# save the doc
doc.save('./test.docx')

任何帮助将不胜感激!

【问题讨论】:

    标签: python python-docx


    【解决方案1】:

    每个单元格在cell.paragraphs 上都有一个或多个段落。任何适用于表格外段落的内容也适用于表格内的段落。

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2020-07-04
      • 2022-08-16
      • 2018-06-30
      • 2022-01-25
      • 1970-01-01
      • 2018-12-15
      • 2021-05-29
      • 2014-04-03
      相关资源
      最近更新 更多