Python使用pyautocad+openpyxl处理cad文件示例

yipeiwu_com5年前Python基础

本文实例讲述了Python使用pyautocad+openpyxl处理cad文件。分享给大家供大家参考,具体如下:

示例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'

其实pyautocad中有关于table的api

示例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'

截取了BM30和BM65的数据

示例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"

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

PyTorch中Tensor的拼接与拆分的实现

拼接张量:torch.cat() 、torch.stack() torch.cat(inputs, dimension=0) → Tensor 在给定维度上对输入的张量序列 s...

django manage.py扩展自定义命令方法

django manage.py扩展自定义命令方法

# django manage.py扩展自定义命令 环境: mac django1.10.3 在实际的项目开发过程中,我们可能要执行某脚本初始化数据库,可能要启动多个服务,比...

python将字符串转换成json的方法小结

最近在工作中遇到了一个小问题,如果要将字符串型的数据转换成dict类型,我第一时间就想到了使用json函数。但是里面出现了一些问题 1、通过json来转换: In [1]: impo...

python验证码识别教程之滑动验证码

前言 上篇文章记录了2种分割验证码的方法,此外还有一种叫做”滴水算法”(Drop Fall Algorithm)的方法,但本人智商原因看这个算法看的云里雾里的,所以今天记录滑动验证码的处...

OpenCV模板匹配matchTemplate的实现

OpenCV模板匹配matchTemplate的实现

作用有局限性,必须在指定的环境下,才能匹配成功,是受到很多因素的影响,所以有一定的适应性 模板匹配是一种最原始、最基本的模式识别方法,研究某一特定对象物的图案位于图像的什么地方,进而识别...