Script to update text and Mtext in Autocad from excel

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()


Comments

Comments are closed on this post.