gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
add hyperlink pydocx
- User
add hyperlink pydocx
- Assistant
To add a hyperlink in a Word document using the pydocx library in Python, you can use the
add_hyperlink()method. Here's an example:from docx import Document from docx.enum.dml import MSO_THEME_COLOR_INDEX def add_hyperlink(paragraph, text, url): part = paragraph.part r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True) hyperlink = docx.oxml.shared.OxmlElement("w:hyperlink") hyperlink.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id"] = r_id new_run = docx.oxml.shared.OxmlElement("w:r") r_pr = docx.oxml.shared.OxmlElement("w:rPr") r_style = docx.oxml.shared.OxmlElement("w:rStyle") r_style.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val"] = "Hyperlink" r_pr.append(r_style) new_run.append(r_pr) new_run.text = text hyperlink.append(new_run) paragraph._p.append(hyperlink) document = Document() paragraph = document.add_paragraph() # Add hyperlink to the paragraph link_text = "Click here" url = "https://www.example.com" add_hyperlink(paragraph, link_text, url) document.save("example.docx")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.