0

I was looking to insert a hyperlink in the text within the DOCX table. Currently the data I have has the hyperlinks in markdown format.

Is there a way to add this as a hyperlink in my DOCx table? I'm aware that this can be done for paragraphs *here, but was wondering it if it could be done in tables.

Example code:

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')

Any help will be greatly appreciated!

Sushiix
  • 33
  • 4

1 Answers1

0

Each cell has one or more paragraphs on cell.paragraphs. Whatever works on a paragraph outside a table will also work on a paragraph inside a table.

scanny
  • 26,423
  • 5
  • 54
  • 80