Python AutoCAD integration in pywin32

Considering the issues we face while working with the pyautocad module or while working with some of the functions provisioned by pyautocad I decided to write a blog post covering win32com, i.e. pywin32. This Python module provides an alternative approach for integrating Python with AutoCAD.

Introducing pywin32, a win32com module for Python

The pywin32 package provides access to many of the Windows APIs from within a Python script. We can interact with different windows applications such as Excel, Word, AutoCAD and many more by using VBA object models for the respective application.

This pywin32 package supports only Python 3 and not the older versions of Python.

To install the pywin32 package I can simply use the Python package manager and the pip install command:

pip install pywin32

Now, to use this package, I have to import the module “win32com.client”.

import win32com.client

VBA object model for AutoCAD

Most of the applications we use in windows have their own object model. AutoCAD VBA (Visual Basic for Applications) has its own object model too.

VBA enables the creation of user-defined functions (UDFs). It furthermore allows for process automatization and provides access to the Windows API as well as other low-level functionality. This is facilitated through dynamic-link libraries (DLLs). Let us take a look at the VBA object model as provided by and described in the AutoCAD documentation.

Figure 1: VBA Object model

Initiating a drawing template using pywin32

Those who have already used VBA for automation purposes in AutoCAD can relate the commands with the object model as shown in Figure 1. For example, Application is the root model at which the actual document is located.

We work on a single AutoCAD template or sheet, which is known as “model space”.

Now, to open a new drawing sheet using VBA, we use the following line of code in VBA:

# pyautocad
import pyautocad
NewDrawing1 = ThisDrawing.Application.Documents.Add("")

To perform the same action with the pywin32 package we can use :

# pywin32
import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application") 
acad.Visible = True
acadModel = acad.ActiveDocument.ModelSpace

Clearly, we can relate the above-mentioned codes as demonstrated below:

To initiate AutoCAD application

# pyautocad


# pywin32
acad = win32com.client.Dispatch("AutoCAD.Application")
acad.Visible = True

To create a new template

# pyautocad


# pywin32

Im my previous blog posts related to pyautocad and AutoCAD scripting in Python I used the method-call Autocad(create_if_not_exists=True) for initiating the application as well as the model space.

In upcoming blog posts I will be discussing how to perform various different tasks and operations using pywin32. I wrote a similar post related to AutoCAD operations with pyautocad in Pyhton as well.

You May Also Like

Leave a Reply


kareemi says:

it does not make any sense.

kareemi, that is not true. Nevertheless, would you mind to be more specific with your feedback? If you need help Tanmay is available for e.g. paid consultation. Have a good day

Anthony Assal says:

Does this method have a better time with regards to call errors from python moving too fast for autocad to keep up with?

Having a fun time playing around with call rejected by callee at the moment and fiddling around with a sleep times between block inserts..

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.