Skip to content

Commit de9bcfd

Browse files
author
Steve Canny
committed
reorg: extract InlineShape to docx.shape
1 parent 0d0501b commit de9bcfd

3 files changed

Lines changed: 69 additions & 53 deletions

File tree

docx/parts/document.py

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
Document parts such as _Document, and closely related classes.
55
"""
66

7-
from docx.enum.shape import WD_INLINE_SHAPE
8-
from docx.opc.constants import RELATIONSHIP_TYPE as RT
9-
from docx.opc.oxml import serialize_part_xml
10-
from docx.opc.package import Part
11-
from docx.oxml.shape import CT_Inline, CT_Picture
12-
from docx.oxml.shared import nsmap, oxml_fromstring
13-
from docx.shared import lazyproperty, Parented
14-
from docx.table import Table
15-
from docx.text import Paragraph
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..opc.constants import RELATIONSHIP_TYPE as RT
12+
from ..opc.oxml import serialize_part_xml
13+
from ..opc.package import Part
14+
from ..oxml.shared import nsmap, oxml_fromstring
15+
from ..shape import InlineShape
16+
from ..shared import lazyproperty, Parented
17+
from ..table import Table
18+
from ..text import Paragraph
1619

1720

1821
class DocumentPart(Part):
@@ -137,47 +140,6 @@ def tables(self):
137140
return [Table(tbl) for tbl in self._body.tbl_lst]
138141

139142

140-
class InlineShape(object):
141-
"""
142-
Proxy for an ``<wp:inline>`` element, representing the container for an
143-
inline graphical object.
144-
"""
145-
def __init__(self, inline):
146-
super(InlineShape, self).__init__()
147-
self._inline = inline
148-
149-
@classmethod
150-
def new_picture(cls, r, image_part, rId, shape_id):
151-
"""
152-
Return a new |InlineShape| instance containing an inline picture
153-
placement of *image_part* appended to run *r* and uniquely identified
154-
by *shape_id*.
155-
"""
156-
cx, cy, filename = (
157-
image_part.default_cx, image_part.default_cy, image_part.filename
158-
)
159-
pic_id = 0
160-
pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
161-
inline = CT_Inline.new(cx, cy, shape_id, pic)
162-
r.add_drawing(inline)
163-
return cls(inline)
164-
165-
@property
166-
def type(self):
167-
graphicData = self._inline.graphic.graphicData
168-
uri = graphicData.uri
169-
if uri == nsmap['pic']:
170-
blip = graphicData.pic.blipFill.blip
171-
if blip.link is not None:
172-
return WD_INLINE_SHAPE.LINKED_PICTURE
173-
return WD_INLINE_SHAPE.PICTURE
174-
if uri == nsmap['c']:
175-
return WD_INLINE_SHAPE.CHART
176-
if uri == nsmap['dgm']:
177-
return WD_INLINE_SHAPE.SMART_ART
178-
return WD_INLINE_SHAPE.NOT_IMPLEMENTED
179-
180-
181143
class InlineShapes(Parented):
182144
"""
183145
Sequence of |InlineShape| instances, supporting len(), iteration, and

docx/shape.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Objects related to shapes, visual objects that appear on the drawing layer of
5+
a document.
6+
"""
7+
8+
from __future__ import (
9+
absolute_import, division, print_function, unicode_literals
10+
)
11+
12+
from docx.enum.shape import WD_INLINE_SHAPE
13+
from docx.oxml.shape import CT_Inline, CT_Picture
14+
from docx.oxml.shared import nsmap
15+
16+
17+
class InlineShape(object):
18+
"""
19+
Proxy for an ``<wp:inline>`` element, representing the container for an
20+
inline graphical object.
21+
"""
22+
def __init__(self, inline):
23+
super(InlineShape, self).__init__()
24+
self._inline = inline
25+
26+
@classmethod
27+
def new_picture(cls, r, image_part, rId, shape_id):
28+
"""
29+
Return a new |InlineShape| instance containing an inline picture
30+
placement of *image_part* appended to run *r* and uniquely identified
31+
by *shape_id*.
32+
"""
33+
cx, cy, filename = (
34+
image_part.default_cx, image_part.default_cy, image_part.filename
35+
)
36+
pic_id = 0
37+
pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
38+
inline = CT_Inline.new(cx, cy, shape_id, pic)
39+
r.add_drawing(inline)
40+
return cls(inline)
41+
42+
@property
43+
def type(self):
44+
graphicData = self._inline.graphic.graphicData
45+
uri = graphicData.uri
46+
if uri == nsmap['pic']:
47+
blip = graphicData.pic.blipFill.blip
48+
if blip.link is not None:
49+
return WD_INLINE_SHAPE.LINKED_PICTURE
50+
return WD_INLINE_SHAPE.PICTURE
51+
if uri == nsmap['c']:
52+
return WD_INLINE_SHAPE.CHART
53+
if uri == nsmap['dgm']:
54+
return WD_INLINE_SHAPE.SMART_ART
55+
return WD_INLINE_SHAPE.NOT_IMPLEMENTED

tests/parts/test_document.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
from docx.oxml.shared import nsmap
1919
from docx.oxml.text import CT_R
2020
from docx.package import ImageParts, Package
21-
from docx.parts.document import (
22-
_Body, DocumentPart, InlineShape, InlineShapes
23-
)
21+
from docx.parts.document import _Body, DocumentPart, InlineShapes
2422
from docx.parts.image import ImagePart
23+
from docx.shape import InlineShape
2524
from docx.table import Table
2625
from docx.text import Paragraph
2726

0 commit comments

Comments
 (0)