Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fff3b0c

Browse files
Benjamin Toornstrascanny
authored andcommitted
opc: add Part.iter_parts_related_by()
1 parent 09400f6 commit fff3b0c

File tree

3 files changed

+121
-72
lines changed

3 files changed

+121
-72
lines changed

docx/opc/part.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def iter_parts_related_by(self, reltypes):
8080
*reltypes* must be a container; `set` is convenient but list or other
8181
sequence types work fine.
8282
"""
83-
return NotImplementedError
83+
return (
84+
rel.target_part for rel in self.rels.values()
85+
if rel.reltype in reltypes
86+
)
8487

8588
@classmethod
8689
def load(cls, partname, content_type, blob, package):

docx/opc/rel.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313

1414
class Relationships(dict):
15-
"""
16-
Collection object for |_Relationship| instances, having list semantics.
17-
"""
15+
"""Collection object for |_Relationship| instances, having dict semantics"""
16+
1817
def __init__(self, baseURI):
1918
super(Relationships, self).__init__()
2019
self._baseURI = baseURI
@@ -132,9 +131,8 @@ def _next_rId(self):
132131

133132

134133
class _Relationship(object):
135-
"""
136-
Value object for relationship to part.
137-
"""
134+
"""Value object for relationship to part"""
135+
138136
def __init__(self, rId, reltype, target, baseURI, external=False):
139137
super(_Relationship, self).__init__()
140138
self._rId = rId
@@ -157,9 +155,12 @@ def rId(self):
157155

158156
@property
159157
def target_part(self):
158+
"""|Part| or subclass this relationship links to."""
160159
if self._is_external:
161-
raise ValueError("target_part property on _Relationship is undef"
162-
"ined when target mode is External")
160+
raise ValueError(
161+
"target_part property on _Relationship is undefined when target mode "
162+
"is External"
163+
)
163164
return self._target
164165

165166
@property

0 commit comments

Comments
 (0)