Home/ Reference/ Layer & Shapes

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

layer.Import(path, filter, options)

Notes

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.ActiveSelectionRange(1) straight away.

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
layer = 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.

Python
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

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:

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

Python
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

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.

Other entries in this section