Skip to content

Feature: Paragraph.text includes hyperlink text #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docx/oxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from __future__ import absolute_import

import re
import six
from lxml import etree

from .ns import NamespacePrefixedTag, nsmap
Expand All @@ -18,14 +20,25 @@
oxml_parser.set_element_class_lookup(element_class_lookup)


def remove_hyperlink_tags(xml):
is_bytestring = isinstance(xml, six.binary_type)
if is_bytestring:
xml = xml.decode('utf-8')
xml = xml.replace('</w:hyperlink>', '')
xml = re.sub('<w:hyperlink[^>]*>', '', xml)
if is_bytestring:
xml = xml.encode('utf-8')
return xml


def parse_xml(xml):
"""
Return root lxml element obtained by parsing XML character string in
*xml*, which can be either a Python 2.x string or unicode. The custom
parser is used, so custom element classes are produced for elements in
*xml* that have them.
"""
root_element = etree.fromstring(xml, oxml_parser)
root_element = etree.fromstring(remove_hyperlink_tags(xml), oxml_parser)
return root_element


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lxml>=3.1.0
mock>=1.0.1
pyparsing>=2.0.1
pytest>=2.5
six>=1.10.0