Repository navigation
How to add a hyperlink to a table. [This is the answer!] #1041
Copy link
Copy link
Open
Labels
hyperlinkRead and write hyperlinks in paragraphRead and write hyperlinks in paragraph
Description
Сала малейкум братья мои, я часов 5 еб**** чтобы найти как так сделать, это решение для вас, чтобы оно отображалось в гугле
Sala maleikum my brothers, I fu**** 5 hours to find how to do this, this is a solution for you so that it is displayed in Google
Dear developers, please contact the clinic for the foreskin to be returned to you and you finally made a normal addition to the table
def add_hyperlink(paragraph, url, text, color):
# This gets access to the document.xml.rels file and gets a new relation id value
part = paragraph.part
r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)
# Create the w:hyperlink tag and add needed values
hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')
hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )
# Create a w:r element
new_run = docx.oxml.shared.OxmlElement('w:r')
rPr = docx.oxml.shared.OxmlElement('w:rPr')
# Join all the xml elements together add add the required text to the w:r element
new_run.append(rPr)
new_run.text = text
hyperlink.append(new_run)
# Create a new Run object and add the hyperlink into it
r = paragraph.add_run ()
r._r.append (hyperlink)
# A workaround for the lack of a hyperlink style (doesn't go purple after using the link)
# Delete this if using a template that has the hyperlink style in it
r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
r.font.underline = True
# 0,122,194
# # Join all the xml elements together add add the required text to the w:r element
# new_run.append(rPr)
# new_run.text = text
# hyperlink.append(new_run)
# paragraph._p.append(hyperlink)
return hyperlink
document = Document()
table = document.add_table(rows=1, cols=4, style='Table Grid')
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '1'
hdr_cells[1].text = '2'
hdr_cells[2].text = '3'
hdr_cells[3].text = '4'
for jira, summary, priority, status in table_object[i]:
row_cells = table.add_row().cells
p_table = row_cells[0].add_paragraph()
hyperlink = add_hyperlink(p_table, f'https://jira.sex.com/browse/{jira}', jira, '#0000FF')
row_cells[1].text = summary
row_cells[2].text = priority
row_cells[3].text = status
row_cells[0].paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
row_cells[2].paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
row_cells[1].paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
row_cells[3].paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
result
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
hyperlinkRead and write hyperlinks in paragraphRead and write hyperlinks in paragraph

Activity
NeilRiver commentedon Jan 26, 2022
if you wanna without spaces in first table
abubelinha commentedon Mar 25, 2023
Thanks. I tried the above code but it didn't work for me:
table = document.add_table(rows=1, cols=4, style='Table Grid')So I removed first indentation level in all lines below that one.
But I guess there is some missing code where
iandtable_objectshould be defined,because I got a new error:
for jira, summary, priority, status in table_object[i]:NameError: name 'table_object' is not definedsixlycos commentedon Apr 7, 2023
@abubelinha hello there, I find this issue and use this way solved my problem.
keypoint is use
p_table = row_cells[0].add_paragraph()to add the cell which u want to add hyperlink.
NeilRiver show the way but did not paste the complete code, you can check my code to solve your problem
abubelinha commentedon Apr 23, 2023
Thanks @sixlycos , it worked except for the underlining option:
My links were never underlined, no matters if I pass
underline=Trueorunderline=FalseSo I changed the
if not underlinecode like this:More underlining style options are available in #19 and in this documentation
(you need to use them changing 1st letter to lowercase:
'wave','double','dottedHeavy', and so on).@abubelinha
Add a heading with a link