我正在使用win32com在AutoCAD中自动执行一些简单的任务。除了能够保存文件之外,它在大多数情况下都运行得很好。我的目标是打开一个(模板)文件,根据需要调整它,然后将该文件保存为另一个文件夹中的.dwg,同时将模板保留为空,以便下次使用。
下面是我的代码示例:
import win32com.client
acad = win32com.client.dynamic.Dispatch("AutoCAD.Application")
acad.Visible=True
doc = acad.Documents.Open("C:\\Template_folder\\Template.dwg")
doc.SaveAs("C:\\Output_folder\\Document1.dwg")
### Adjust dwg ###
doc.Save()加载模板文件运行良好,但在尝试保存文件时(使用SaveAs时,我得到以下错误:
doc.SaveAs("C:\\Output_folder\\Document1.dwg")
File "<COMObject Open>", line 3, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'AutoCAD', 'Error saving the document', 'C:\\Program Files\\Autodesk\\AutoCAD 2019\\HELP\\OLE_ERR.CHM', -2145320861, -2145320861), None)任何技巧或资源都将非常感谢!
发布于 2019-11-04 22:16:43
查看AutoCAD的ActiveX API的文档,当您调用Documents.Open()时,它应该返回打开的文档并将其设置为活动文档。这就是说,看起来这并不是这里实际发生的事情。您的问题的解决方案应该如下所示:
import win32com.client
acad = win32com.client.dynamic.Dispatch("AutoCAD.Application")
acad.Visible=True
# Open a new document and set it as the active document
acad.Documents.Open("C:\\Template_folder\\Template.dwg")
# Set the active document before trying to use it
doc = acad.ActiveDocument
# Save the documet
doc.SaveAs("C:\\Output_folder\\Document1.dwg")
### Adjust dwg ###
doc.Save()您可以在此处找到文档
https://stackoverflow.com/questions/58450655
复制相似问题