【发布时间】: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