Skip to content

How to add a hyperlink to a table. [This is the answer!] #1041

Open
@NeilRiver

Description

@NeilRiver

Сала малейкум братья мои, я часов 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

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    hyperlinkRead and write hyperlinks in paragraph

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions