超级干货-python批量修改替换cad图纸文本

本文介绍了一种使用Python和pyautocad库的方法,通过遍历指定目录下的dwg文件,批量查找并替换CAD图纸中的特定文本,如将所有x批改为14批。
摘要由CSDN通过智能技术生成

超级干货-python批量修改替换cad图纸文本

from pyautocad import Autocad,APoint
import math
import os 
import os.path as osp
import re
 
reg=re.compile(r'.*(.dwg)$')#匹配dwg文件
path=r"F:\22年14批预算明细"
lst=[]
for dirpath,dirname,filename in os.walk(path):
    li=[osp.join(dirpath,s) for s in filename if osp.isfile(osp.join(dirpath,s))
                                              if reg.findall(s)]
    for i in li:
        if i:
            lst.append(i)
 
for filename in lst:
    #连接CAD
    acad=Autocad(create_if_not_exists=True,visible=True)#create_if_not_exists—如果CAD不运行,则会创建新的实例.visible -如果为True,新AutoCAD实例将可见(默认)
    #打开/读取CAD文件
    acad.ActiveDocument.Application.Documents.open(filename)
    acad.prompt("Hello! ZWCAD from pyautocad.")
    print(acad.doc.Name)
 
    #遍历CAD文件中的所有文本对象,打印其文本内容及坐标位置
    for text in acad.iter_objects("Text"):#遍历Text对象
        #修改对象属性,将文本中的"x批"字符替换为"14批"
        if 'x批' in text.TextString:
            print('text: %s at: %s' %(text.TextString,text.InsertionPoint))
            # 修改對象屬性
            text1 = str(text.TextString)
            text1 = text1.replace('x批','14批')
            text.TextString = text1
            acad.ActiveDocument.SaveAs(filename, 61)#同名另存
 
    acad.ActiveDocument.Close()#关闭当前
           
 
acad.app.Quit()#退出cad程序
 
 
         
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值