Skip to content

Instantly share code, notes, and snippets.

@thengineer
Created October 25, 2013 16:27
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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()
@LiaungYip
Copy link

You are a prince. Thank you.

@johnjat
Copy link

johnjat commented Nov 2, 2016

How do i add the win32 module in my python

@AndresRomero2332
Copy link

Hi I don't know how to create blocks with atributes, maybe someone here could help me please??

@lesu90
Copy link

lesu90 commented Mar 26, 2022

Hi, thanks for this code.
I have blocks with this attributes:
ITEM_CODE: AAAAA
WEIGHT: BBBB
NAME_LOCAL: CCCC

I can already print them and see them on the screen, but how can I extract them to work with them?

@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