Working with AutoCAD in Excel and Python

In this article I will demonstrate how data from Excel sheets can be used to draw objects on AutoCAD using Python.

Libraries for working with Excel and AutoCAD in Python

For this particular demonstration, I am using pyautocad and the openpyxl Python libraries. The openpyxl Python module helps me fetch data from an existing Excel files.

from pyautocad import Autocad, APoint, aDouble
import openpyxl as oex

Fetching data from Excel files for AutoCAD in Python

To fetch data from Excel files I need two variables. The first one is used to store the workbook. The second variable is used to store the currently active worksheet. See below code for clarification.

wbook = oex.load_workbook("Test.xlsx")
sh =

In this basic example, I will just connect some random points using the AddPolyline method from pyautocad. The points are specified in my Excel file. The Excel file takes the form of a table. I show it in the screenshot displayed below.

Excel sheet for AutoCAD polyline with pyautocad in Python

From the articles I published earlier, I know that in order to create a AutoCAD polyline I need a 3-element array of datatype double. This is why I need to iterate over each rows in my table while adding it to a list. I clarify this in below code.

l1 = []
for i in range(2, sh.max_row+1):
    for j in range(2, sh.max_column+1):
        cell = sh.cell(row=i, column=j)

[125, 235, 0, 670, 217, 0, 105, 756, 0, 654, 789, 0, 125, 235, 0]

Now that I have created the list I can use it for creating bespoken AutoCAD polyline.

Creating AutoCAD polyline with pyautocad

Finally, I need to convert the list into a aDouble (Array of doubles) object. This allows me to use it as a input parameter when calling the AddPolyline pyautocad method. This is the method that will actually draw the AutoCAD polyline. The code is displayed below.

polygon = aDouble(l1)

And this is what was drawn in AutoCAD after executing above Python code.

AutoCAD poyline drawn with Excel data in Python

As can be seen from the picture above a polyline was created, in accordance with the data specified in my Excel file.

Concluding remarks

Coming to end, I can say that when using the concept described by me in this article AutoCAD users can use data from Excel for AutoCAD object creation in Python. Besides openpyxl, Python developers can also consider using pandas. pandas is one of the trending Python libraries for dealing with Excel files. It supports analytics as well.

In one of my future AutoCAD blog posts I will discuss how to work with the AutoCAD Table object using Python. To learn more about such AutoCAD automation techniques, I would recommend you to go through my previous articles. Also, we are open for technical consultation if contacted using our contact form. In case of any questions feel free to use our comment box.

Other content covering Python for AutoCAD

I have already published a few articles covering pyautocad, AutoCAD, pythoncom, and pywin32. So please see a list of some related content below:

You May Also Like

Leave a Reply

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.