Skip to content

Instantly share code, notes, and snippets.

@thengineer
Created October 25, 2013 16:27
Show Gist options
  • Save thengineer/7157510 to your computer and use it in GitHub Desktop.
Save thengineer/7157510 to your computer and use it in GitHub Desktop.
AutoCAD automation via python and win32com: read all attributes from BlockReferences
import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument # Document object
# iterate trough all objects (entities) in the currently opened drawing
# and if its a BlockReference, display its attributes and some other things.
for entity in acad.ActiveDocument.ModelSpace:
name = entity.EntityName
if name == 'AcDbBlockReference':
HasAttributes = entity.HasAttributes
if HasAttributes:
print(entity.Name)
print(entity.Layer)
print(entity.ObjectID)
for attrib in entity.GetAttributes():
print(" {}: {}".format(attrib.TagString, attrib.TextString))
# update text
attrib.TextString = 'modified with python'
attrib.Update()
@Bozidarmb
Copy link

Hi, thengineer!

First of all, thx for such a good code.
Could you PLEASE help me to find a way to solve my problem:
need to change #acad.ActiveDocument.PaperSpace#, so the script can look through all layouts in dwg file and do the job

@lam2912
Copy link

lam2912 commented Jun 8, 2023

Dear thengineer!
I use Visual Studio Code. But i can't setup Module WIN32, PYCOM.
Please tell me how i can do?
Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment