Python Musings

Statistics

  • Entries (2)
  • Comments (0)

Categories

Script to update text and Mtext in Autocad from excel 

Posted by run8tech Thursday, December 1, 2016 3:15:00 PM Categories: AutoCAD,Python

Loosely based on the last post where I scripted out all the text from a drawing now here is a script to put it back in.

 

This is handy if you want to make changes in an Excel environment and then push it back.

 

Note: I only implement AcDbText and AcDbMtext.  If you need the block attributes as well let me know

 

 

from __future__ import print_function
import os
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
from xlwt import Workbook

from pyautocad import Autocad, APoint


#need to do: open all drawings in a directory
fname = join(dirname(dirname(abspath(__file__))), 'test_data', 'C:/temp/import.xls')

#Open the workbook
xl_workbook = xlrd.open_workbook("Import.xls")
sheet_names = xl_workbook.sheet_names()

xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])

wb = copy(xl_workbook)
sheet = wb.get_sheet(0)

#dwgfiles = filter(os.path.isfile, os.listdir( os.curdir ) )
 
cwd = os.path.abspath(os.path.curdir) #current working dir


idx = 1

 
import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")   
    
lastdrawing = ""
acad = Autocad()
acad.docs.Open(cwd + "/" + "Template.dwg")  #template file goes here
                


doc = acad.ActiveDocument   # Document object


print ("MODEL SPACE")

idx = 0
for entity in acad.ActiveDocument.ModelSpace:
    name = entity.EntityName
    if name == 'AcDbText' or name == 'AcDbMText':
        
        print (entity.ObjectID)
        print (entity.TextString)
        
        a = entity.ObjectID
            
        b = xl_sheet.cell(idx, 0).value
        c = entity.TextString
                       
        entity.TextString = b
        idx = idx + 1  
        

#acad.doc.Save()
#acad.doc.Close()


Script to Extract All Text from a Folder of AutoCAD files to Excel 

Posted by run8tech Tuesday, November 29, 2016 8:09:00 AM

Python script to iterate through all of the drawing (.dwg) files in a directory the script is in and create a spreadsheet with the 

Text (either from text object or block attribute)

ObjectID (from text object or block)

Path and dwg file.

from both Paper Space and Model Space.

It should be easy to modify the code to search for a particular named block (Like a Titleblock for instance)

Note: to run if you have Python installed.  Copy code below and save it as a .py file.  Throw it in directory with cad files and double click. 

AUTOCAD MUST BE OPEN IN ORDER FOR THIS TO WORK

 

from __future__ import print_function
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
import xlwt 
from pyautocad import Autocad, APoint
import os
import win32com.client

#Create workbook
book = xlwt.Workbook()
ws = book.add_sheet("ExportedData")
book.save("Exported.xls")
          
#Open the workbook
xl_workbook = xlrd.open_workbook("Exported.xls")
sheet_names = xl_workbook.sheet_names()

xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])

wb = copy(xl_workbook)
sheet = wb.get_sheet(0)

dwgfiles = filter(os.path.isfile, os.listdir( os.curdir ) )
 
cwd = os.path.abspath(os.path.curdir) #current working dir

for f in dwgfiles:
    if f.endswith(".dwg"):
        """ open Document"""
        acad = Autocad()
        acad.docs.Open(cwd + "/" + f)

        print (acad.doc.Name)

        num_cols = xl_sheet.ncols   # Number of columns
        idx = 1
        
        acad = win32com.client.Dispatch("AutoCAD.Application")

        doc = acad.ActiveDocument   # Document object


        print ("MODEL SPACE")

        for entity in acad.ActiveDocument.ModelSpace:
            name = entity.EntityName
            if name == 'AcDbText':
                sheet.row(idx).write(0,entity.TextString) 
                sheet.row(idx).write(1,entity.ObjectID)
                sheet.row(idx).write(2,cwd + "/" + f)
                idx = idx + 1
            if name == 'AcDbBlockReference':
                HasAttributes = entity.HasAttributes
                if HasAttributes:
                  #  print(entity.Name)
               #     print(entity.Layer)
                #    print(entity.ObjectID)
                    for attrib in entity.GetAttributes():
             
                        if attrib.TagString != "DATA":
                            sheet.row(idx).write(0,attrib.TextString ) 
                            sheet.row(idx).write(1,entity.ObjectID)
                            sheet.row(idx).write(2,cwd + "/" + f)
                            idx = idx + 1

                        
                        
        print ("PAPER SPACE")
        for entity in acad.ActiveDocument.PaperSpace:
            name = entity.EntityName
            if name == 'AcDbText':
                sheet.row(idx).write(0,entity.TextString) 
                sheet.row(idx).write(1,entity.ObjectID)
                sheet.row(idx).write(2,cwd + "/" + f)
                idx = idx + 1
            if name == 'AcDbBlockReference':
                HasAttributes = entity.HasAttributes
                if HasAttributes:
                  #  print(entity.Name)
               #     print(entity.Layer)
                #    print(entity.ObjectID)
                    for attrib in entity.GetAttributes():
         
                        if attrib.TagString != "DATA":
                            sheet.row(idx).write(0,attrib.TextString ) 
                            sheet.row(idx).write(1,entity.ObjectID)
                            sheet.row(idx).write(2,cwd + "/" + f)
                    
            
                        idx = idx + 1
                        

        
        doc.Close(False)
        acad = None
wb.save("Exported.xls")