Title: GitHub · Where software is built
Open Graph Title: python-openxml/python-docx
X Title: python-openxml/python-docx
Description: Create and modify Word documents with Python. Contribute to python-openxml/python-docx development by creating an account on GitHub.
Open Graph Description: Create and modify Word documents with Python. Contribute to python-openxml/python-docx development by creating an account on GitHub.
X Description: Create and modify Word documents with Python. Contribute to python-openxml/python-docx development by creating an account on GitHub.
Opengraph URL: https://github.com/python-openxml/python-docx
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Feature Request: Add Hyperlinks With Multiple Runs","articleBody":"I need to be able to add hyperlinks with multiple runs to paragraphs. Right now I am able to achieve this like so:\n```\nfrom docx.oxml.shared import OxmlElement, qn\nfrom docx.opc.constants import RELATIONSHIP_TYPE\nfrom docx import Document\nfrom docx.text.run import Run\nfrom docx.shared import RGBColor\n\ndef add_hyperlink(paragraph, address, runs):\n # Access the document.xml.rels file and get a new relationship type ID\n part = paragraph.part\n r_id = part.relate_to(address, RELATIONSHIP_TYPE.HYPERLINK, is_external=True)\n\n # Create the w:hyperlink tag and add needed properties\n HYPERLINK_TAG = 'w:hyperlink'\n RELATIONSHIP_TYPE_ID_PROPERTY = 'r:id'\n hyperlink = OxmlElement(HYPERLINK_TAG)\n hyperlink.set(qn(RELATIONSHIP_TYPE_ID_PROPERTY), r_id)\n\n for run in runs:\n hyperlink.append(run)\n\n paragraph._p.append(hyperlink)\n return hyperlink\n\nRUN_TAG = 'w:r'\nPROPERTIES_TAG = 'w:rPr'\ndocument = Document()\nparagraph = document.add_paragraph()\n\nr1 = OxmlElement(RUN_TAG)\nrPr1 = OxmlElement(PROPERTIES_TAG)\nr1.append(rPr1)\nr1.text = 'Google '\nrun1 = Run(r1, paragraph)\nrun1.underline = True\nrun1.bold = True\nrun1.font.color.rgb = RGBColor(255, 0, 0)\n\nr2 = OxmlElement(RUN_TAG)\nrPr2 = OxmlElement(PROPERTIES_TAG)\nr2.append(rPr2)\nr2.text = 'dot com'\nrun2 = Run(r2, paragraph)\nrun2.underline = True\nrun2.italic = True\nrun2.font.color.rgb = RGBColor(0, 255, 0)\n\nadd_hyperlink(paragraph, 'https://google.com', [run1._r, run2._r])\ndocument.save('new.docx')\n```\nand this produces a hyperlink that looks like this:\n\n\u003cimg width=\"361\" height=\"99\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/735be24b-2949-4847-8e6d-68627326434a\" /\u003e\n\nWould it be possible to extend the API to support adding hyperlinks with different runs? Since hyperlinks are containers for runs, similar to paragraphs, I would imagine the API would look similar to the API for paragraphs. I.e. you can add a hyperlink with text directly:\n```\nhyperlink = paragraph.add_hyperlink('https://google.com', 'Google dot com')\n```\nYou can change the text and address post-factum:\n```\nhyperlink.text = 'Google.com'\nhyperlink.address = 'https://www.google.com'\n```\nOr you can fill the hyperlink incrementally with runs:\n```\nhyperlink = paragraph.add_hyperlink('https://google.com')\nhyperlink.add_run('Google ').bold = True\nhyperlink.add_run('dot com').italic= True\n```\n\nIt would be nice to have this functionality in the API by default. I can try to add it if you tell me what you'd like the API to look like. Thanks in advance!\n","author":{"url":"https://github.com/ptoshkov","@type":"Person","name":"ptoshkov"},"datePublished":"2025-08-31T17:12:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1515/python-docx/issues/1515"}
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:c5a925d3-fc04-25f4-9b49-17c6a8e4abb8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 85FA:3AD8E1:C3BE3F:1034274:69716BAA |
| html-safe-nonce | 93a55436761407594bb814958843db1ded4f81274b31329a7d573b6b3085e04f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NUZBOjNBRDhFMTpDM0JFM0Y6MTAzNDI3NDo2OTcxNkJBQSIsInZpc2l0b3JfaWQiOiI5NzM5NzU3ODcwOTE4ODA4NzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f033d1f5fb5f8452cdaeeceff69f6f11b28e00654e448fdf1cce57963b958a97 |
| hovercard-subject-tag | repository:13592924 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-openxml/python-docx/issues/1515 |
| twitter:image | https://opengraph.githubassets.com/f2d329a0f7c9f32518a4a67a5f258660f1f5b118668ce5fa4cd2d914e65005fd/python-openxml/python-docx |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f2d329a0f7c9f32518a4a67a5f258660f1f5b118668ce5fa4cd2d914e65005fd/python-openxml/python-docx |
| og:image:alt | Create and modify Word documents with Python. Contribute to python-openxml/python-docx development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 353479110ec1e87d687013548f216c852055ec907d09d2fb6a180cb5ed9d28e1 |
| turbo-cache-control | no-cache |
| go-import | github.com/python-openxml/python-docx git https://github.com/python-openxml/python-docx.git |
| octolytics-dimension-user_id | 3403760 |
| octolytics-dimension-user_login | python-openxml |
| octolytics-dimension-repository_id | 13592924 |
| octolytics-dimension-repository_nwo | python-openxml/python-docx |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 13592924 |
| octolytics-dimension-repository_network_root_nwo | python-openxml/python-docx |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a4c7e274c03a115b82dcef6c47ce3f7f903e88e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width