layer.Import(path, filter, options)
Imports an image, SVG or other external file. 0 means auto-detect the format; None means default options. The new object comes back selected, so grab it with app.Act
Signature
Notes
0 means auto-detect the format; None means default options. The new object comes back selected, so grab it with app.ActiveSelectionRange(1) straight away.Python example (runnable)
pywin32. The example assumes app / doc / page / layer are already connected.
See Connection & Initlayer = doc.ActivePage.ActiveLayer
# Import(path, filter, options): 0 = detect format, None = default options
# the imported object is selected for you
layer.Import("D:/images/photo1.jpg", 0, None)
img = app.ActiveSelectionRange(1) # grab the image that just came in
print("Imported, size: ", img.SizeWidth, "x", img.SizeHeight)
It returns a shape range, not a single shape
layer.Import gives you a ShapeRange, not a Shape. Most
of the time there is exactly one member inside — one JPEG, one PNG — but that is not guaranteed.
Import an SVG and you may get a whole group of shapes; import a grouped AI file and you may get
several objects. Code written on the assumption that there is always exactly one will
eventually break on some client's file.
rng = layer.Import("D:/images/logo.svg", 0, None)
print(rng.Count) # may be 1, may be 12
for i in range(1, rng.Count + 1):
s = rng.Item(i) # collections are 1-based
print(s.Type, s.Name)
The three arguments
FileName— full path. Forward slashes work on Windows.Filter— pass0to detect from the extension. This is the one number worth memorising; unlike export filters it needs no probing, and automatic detection covers nearly every file type you will meet.Options— an import options object, normallyNone.
If the extension lies about the real format — a PNG saved as .jpg — detection can
fail or pick the wrong filter. Only then do you need a specific number, and you find it the same way
you find export filter numbers.
Import is heavy work, and it lands later than you think
Importing a large TIFF can return before the file is fully decoded. Reading
rng.Item(1).SizeWidth immediately afterwards occasionally yields 0 or a stale value.
That is not a bug, it is asynchronous writing. A time.sleep(0.2) before reading makes
the symptom disappear. In a batch import this delay is not optional, for the same reason given in
throttling.
Import overwrites the selection
Importing makes the new object the current selection. Two consequences:
- If you had selected something else, that selection is gone. If you still need it, hold a reference in a variable, or finish that work before importing.
- To "grab what just came in", use the return value. Do not rely on
doc.ActiveSelectionRange, because the next operation of any kind can change the selection.
A missing file raises; it does not return empty
This differs from export. Export tends to return False on failure, while import with a
non-existent path usually raises DISP_E_EXCEPTION (-2147352567,
0x80020009), with a more specific description in e.args[2]. Checking
os.path.exists up front is less work than reading that exception afterwards.
Complete example
import os, time
def import_into(layer, path, filt=0):
if not os.path.exists(path):
raise FileNotFoundError(path) # fail loudly, before touching COM
rng = layer.Import(path, filt, None) # filt=0 means detect from the extension
time.sleep(0.25) # decoding is not finished when it returns
n = rng.Count if rng else 0
if n == 0:
raise RuntimeError(f"import produced nothing: {path}")
return rng # a ShapeRange, possibly with several members
rng = import_into(doc.ActivePage.ActiveLayer, "D:/images/photo1.jpg")
print("imported", rng.Count, "shape(s)")
Two things to know about importing PDF and AI
- Only the first page comes in. A multi-page PDF imported this way gives you the contents of page one. Walking every page needs a different route.
- Fonts may be substituted. If the text in an AI or PDF file has no matching font installed, it is replaced on import, and glyph positions and line widths shift. This is the most common reason for "the layout changed after importing", and it has nothing to do with your script.
Import versus open
layer.Import puts content into the active layer of the current
document; the document stays the same one. app.OpenDocument opens a
new document. To merge an outside file into existing artwork, use the former; to
work on a standalone file, use the latter. Mixing the two makes a later
doc.ActivePage point at something you did not expect — a very common class of bug in
batch scripts.
About this section: Layer & Shapes
Layer & Shapes — layers and shape creation.
- This section has 14 entries; this is one of them.
- See the full section index: Layer & Shapes
Other entries in this section
- layer.CreateRectangle2(x, y, w, h)Draws a rectangle. (x, y) is the bottom-left corner; w and h are width
- layer.CreateRectangle(l, t, r, b)Rectangle from four edges. t (top) must be a larger number than b (bot
- layer.CreateEllipse2(x, y, w, h)Ellipse from bottom-left corner plus size. w == h gives a circle.
- layer.CreateEllipse(l, t, r, b)Ellipse from four edges.
- layer.CreateArtisticText(left, top, text, lang, charset, font, size, bold, italic, underline, align)Creates artistic text (a single-line heading). size is in points, not
- layer.CreateParagraphText(left, top, right, bottom, text, lang, charset, font, size, bold, italic, underline, align)Creates paragraph text that wraps inside a box. Requirement: top > bot
- layer.CreateCurve(...) / layer.CreateFreehandCurve(...)Builds a curve from an array of points. Used for procedural artwork (f
- layer.Activate()Makes this the active layer.