from pyautocad import Autocad,APoint
import csv
import re
# 连接CAD程序
acad=Autocad(create_if_not_exists=True,visible=True)
filename=r'G:\桌面\导出cad表格数据\模板竣工图.dwg'
#打开指定路径下的 CAD 文件并将其作为当前文档
doc =acad.ActiveDocument.Application.Documents.open(filename)
#当前模型空间
mspace = doc.ModelSpace
# 遍历模型空间中的对象
for obj in mspace:
if obj.ObjectName== "AcDbTable":
table = obj
style_name = table.StyleName
# 提取表格数据
rows = table.Rows
cols = table.Columns
data = []
for i in range(rows):
row_data = []
for j in range(cols):
cell = table.GetText(i, j)
ls=re.split(';',cell.strip(r'{}'))
row_data.append(ls[-1])
data.append(row_data)
# 导出表格数据
with open(r'table.csv', "w", newline="", encoding="utf-8-sig") as csvfile:
writer = csv.writer(csvfile)
for row in data:
writer.writerow(row)