Python Using pyautocad+openpyxl to Process cad Files Example

  • 2021-07-13 06:00:46
  • OfStack

This article illustrates how Python uses pyautocad+openpyxl to process cad files. Share it for your reference, as follows:

Example 1:


from pyautocad import Autocad
import openpyxl
wb=openpyxl.load_workbook('./cads.xlsx')
sheet=wb.get_sheet_by_name('Sheet1')
data=[]
pset=[]
acad=Autocad(create_if_not_exists=True)
acad.prompt('hello this is python in')
for text in acad.iter_objects('Text'):
 data.append(text.TextString)
from pyautocad import APoint
for text in acad.iter_objects('Text'):
 pset.append(APoint(text.InsertionPoint))
print len(data)
for d in range(1,len(data)):
 sheet['A'+str(d)].value=data[d]
 sheet['B'+str(d)].value=str(pset[d].x)
 sheet['C'+str(d)].value=str(pset[d].y)
wb.save('aabb1.xlsx')
print 'success aabb1.xlsx'

In fact, there is api about table in pyautocad

Example 2:


from pyautocad import Autocad
import openpyxl
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
wb=openpyxl.load_workbook('./aabb.xlsx')
sheet=wb.get_sheet_by_name('Sheet1')
data=[]
acad=Autocad(create_if_not_exists=True)
acad.prompt('hello this is python in')
for text in acad.iter_objects('Text'):
 data.append(text.TextString)
print len(data)
for d in range(1,len(data)):
 if(str(data[d])[0:4]=="BM30" or str(data[d])[0:4]=="BM65"):
  sheet['A'+str(d)].value=data[d]
wb.save('ky1.xlsx')
print 'success ky1.xlsx'

Intercepted the data of BM30 and BM65

Example 3:


import openpyxl
from pyautocad import Autocad,APoint
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
wb=openpyxl.load_workbook("a.xlsx")
sheet=wb.get_sheet_by_name("Sheet1")
data=[]
px=[]
py=[]
acad=Autocad(create_if_not_exists=True)
acad.prompt("hello this is mt")
for text in acad.iter_objects('Text'):
  data.append(text.TextString)
  #print text.TextString
  px.append(APoint(text.InsertionPoint).x)
  py.append(APoint(text.InsertionPoint).y)
  #print text.InsertionPoint
print len(data)
print "eof"
for d in range(1,len(data)):
  if(str(data[d])[0:4]=="Vigi" or str(data[d])[0:4]=="iC65" or str(data[d])[0:3]=="CVS" or str(data[d])[0:3]=="PRD" or str(data[d])[0:4]=="DDZY"):
    sheet['A'+str(d)]=data[d]
    sheet['B'+str(d)]=px[d]
    sheet["C"+str(d)]=py[d]
   #  print data[d]
wb.save("kv.xlsx")
print "success"
#or str(data[d])[0:3]=="CVS" or str(data[d])[0:3]=="PRD" or str(data[d])[0:4]=="DDZY"

For more readers interested in Python related contents, please check the topics of this site: "Summary of Python File and Directory Operation Skills", "Summary of Python Text File Operation Skills", "Python Data Structure and Algorithm Tutorial", "Summary of Python Function Use Skills", "Summary of Python String Operation Skills" and "Introduction and Advanced Classic Tutorial of Python"

I hope this article is helpful to everyone's Python programming.


Related articles: