import os
import os.path as osp
#运行cad程序
acad = Autocad(create_if_not_exists=True, visible=True)
path = os.getcwd()
for dirpath,_,filename in os.walk(path):
dwgs=[osp.join(dirpath,s) for s in filename if s.endswith(('dwg','DWG'))]
for dwg_filename in dwgs:
# 读取cad文件
acad.app.Documents.Open(dwg_filename)
doc = acad.ActiveDocument # 设置为当前活动文档
layers = doc.Layers
doc.ActiveLayer = layers.Item(0)
for obj in doc.ModelSpace:
obj.Layer = layers.Item(0).Name
obj.Color = 16777215#
if obj.EntityName in ['AcDbText', 'AcDbMText']:#判断对象是否是单行文本对象(AcDbText)或多行文本对象(AcDbMText)
obj.StyleName = "宋体"
doc.Save()
doc.Close()
acad.app.Quit()