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 = wbook.active
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.
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) l1.append(cell.value) print(l1) O/p: [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) acad.model.AddPolyline(polygon)
And this is what was drawn in AutoCAD after executing above Python code.
As can be seen from the picture above a polyline was created, in accordance with the data specified in my Excel file.
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:
- Link: Extending the objects in AutoCAD using pyautocad in Python
- Link: Using Python lists and dictionaries to work with AutoCAD objects with pyautocad
- Link: Hatching objects on AutoCAD template using pywin32 in Python
- Link: Raster image object in AutoCAD with pyautocad in Python
- Link: Working with 3D mesh object in AutoCAD using pyautocad in Python
- Link: Creating adouble constructor using pywin32 in Python
- Link: Creating apoint method using pywin32 in Python
- Link: Python integration with AutoCAD using pywin32 and win32com
- Link: Deleting objects in a AutoCAD template with pyautocad and pywin32 in Python
- Link: Mirror object on a 2D plane with pyautocad in Python
- Link: Working with texts in Autocad using pyautocad in Python
- Link: Polar arrays in AutoCAD using pyautocad in Python
- Link: Rectangular arrays in AutoCAD using pyautocad in Python
- Link: Operations with AutoCAD objects using pyautocad in Python
- Link: Solid objects in AutoCAD using pyautocad in Python
- Link: Working with helices in AutoCAD using pyautocad in Python
- Link: Drawing splines in AutoCAD with pyautocad in Python
- Link: Polylines in pyautocad for drawing AutoCAD polygons in Python
- Link: Drawing ellipse arcs in AutoCAD using pyautocad in Python
- Link: Drawing arcs in AutoCAD using pyautocad in Python
- Link: Near simultaneous factory design and process optimization with Promodel AutoCAD edition
- Link: Python for AutoCAD pyautocad module
- Link: Region object in AutoCAD with Python
- Link: AutoCAD Application object class in Python
- Link: AutoCAD Document object in Python
- Link: AutoCAD Block object in Python
- Link: AutoCAD Attribute object in Python
- Link: DimAligned object in AutoCAD using Python
- Link: SelectionSet object in AutoCAD with Python
Civil engineer interested in automation in core subjects such as civil, mechanical and electrical, using IT skills comprising cloud computing, devops, programming languages and databases along with the technical skills gained while working as a civil engineer since past 3 years.
Leave a Reply