Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyautocad是否需要运行的autoCad软件?_Python_Python 3.x_Autocad - Fatal编程技术网

Python pyautocad是否需要运行的autoCad软件?

Python pyautocad是否需要运行的autoCad软件?,python,python-3.x,autocad,Python,Python 3.x,Autocad,我犯了一个错误。当我尝试将dwg AutoCAD文件转换为excel时。 我曾经 我犯了一个错误 这是我运行的脚本。autocad是否正在运行是使用此库pyautocad所必需的 from __future__ import print_function from os.path import join, dirname, abspath from xlutils.copy import copy import xlrd import xlwt from pyautocad import Au

我犯了一个错误。当我尝试将dwg AutoCAD文件转换为excel时。 我曾经 我犯了一个错误

这是我运行的脚本。autocad是否正在运行是使用此库pyautocad所必需的

from __future__ import print_function
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
import xlwt 
from pyautocad import Autocad, APoint
import os
import win32com.client
from pyautocad import Autocad, APoint
from pyautocad.contrib.tables import Table
#Create workbook
book = xlwt.Workbook()
ws = book.add_sheet("ExportedData")
book.save("Exported.xls")

#Open the workbook
xl_workbook = xlrd.open_workbook("Exported.xls")
sheet_names = xl_workbook.sheet_names()

xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])

wb = copy(xl_workbook)
sheet = wb.get_sheet(0)

dwgfiles = filter(os.path.isfile, os.listdir( os.curdir ) )

cwd = os.path.abspath(os.path.curdir) #current working dir

for f in dwgfiles:
  print(f)
  if f.endswith(".dwg"):
      print("sdaasdas")
      """ open Document"""
      acad = Autocad()
      print(cwd)
      acad.doc.Open(cwd + "/" + f)

      print (acad.doc.Name)

      num_cols = xl_sheet.ncols   # Number of columns
      idx = 1

      acad = win32com.client.Dispatch("AutoCAD.Application")

      doc = acad.ActiveDocument   # Document object


      print ("MODEL SPACE")

      for entity in acad.ActiveDocument.ModelSpace:
          name = entity.EntityName
          print(name)
          if name == 'AcDbText':
              sheet.row(idx).write(0,entity.TextString) 
              sheet.row(idx).write(1,entity.ObjectID)
              sheet.row(idx).write(2,cwd + "/" + f)
              idx = idx + 1
          if name == 'AcDbBlockReference':
              HasAttributes = entity.HasAttributes
              if HasAttributes:
                #  print(entity.Name)
             #     print(entity.Layer)
              #    print(entity.ObjectID)
                  for attrib in entity.GetAttributes():

                      if attrib.TagString != "DATA":
                          sheet.row(idx).write(0,attrib.TextString ) 
                          sheet.row(idx).write(1,entity.ObjectID)
                          sheet.row(idx).write(2,cwd + "/" + f)
                          idx = idx + 1



      print ("PAPER SPACE")
      for entity in acad.ActiveDocument.PaperSpace:
          name = entity.EntityName
          if name == 'AcDbText':
              sheet.row(idx).write(0,entity.TextString) 
              sheet.row(idx).write(1,entity.ObjectID)
              sheet.row(idx).write(2,cwd + "/" + f)
              idx = idx + 1
          if name == 'AcDbBlockReference':
              HasAttributes = entity.HasAttributes
              if HasAttributes:
                #  print(entity.Name)
             #     print(entity.Layer)
              #    print(entity.ObjectID)
                  for attrib in entity.GetAttributes():

                      if attrib.TagString != "DATA":
                          sheet.row(idx).write(0,attrib.TextString ) 
                          sheet.row(idx).write(1,entity.ObjectID)
                          sheet.row(idx).write(2,cwd + "/" + f)


                      idx = idx + 1



      doc.Close(False)
      acad = None
wb.save("Exported.xls")
当我运行这个脚本时,我得到的错误是

File "test.py", line 36, in <module>
  acad.doc.Open(cwd + "/" + f)
File "C:\Users\nithish\AppData\Local\Programs\Python\Python37\lib\site-packages\pyautocad\api.py", line 74, in doc
  return self.app.ActiveDocument
File "C:\Users\nithish\AppData\Local\Programs\Python\Python37\lib\site-packages\pyautocad\api.py", line 63, in app
  self._app = comtypes.client.GetActiveObject('AutoCAD.Application', dynamic=True)
File "C:\Users\nithish\AppData\Local\Programs\Python\Python37\lib\site-packages\comtypes\client\__init__.py", line 173, in GetActiveObject
  clsid = comtypes.GUID.from_progid(progid)
File "C:\Users\nithish\AppData\Local\Programs\Python\Python37\lib\site-packages\comtypes\GUID.py", line 78, in from_progid
  _CLSIDFromProgID(str(progid), byref(inst))
File "_ctypes/callproc.c", line 933, in GetResult
OSError: [WinError -2147221005] Invalid class string

您将得到以下错误:

OSError:[WinError-2147221005]无效的类字符串

如果

您没有安装AutoCAD 如果您安装了一个版本,但试图实例化错误的版本,如:CreateObjectAutocad19.Application(当您安装AutoCAD20时)。 通常,库会省略版本号,因此Autocad.Application可以正常工作


如果安装了Autocad实例,则对Autocad的调用将启动该实例。

不,您不需要运行Autocad。但请确保正确安装。@Arnie97在安装AutoAdd:\autocad\U文件后仍会出现错误回溯上次调用:文件test.py,第37行,位于acad.doc.Opencwd+/+f文件C:\Users\nithish\AppData\Local\Programs\Python 37\lib\site packages\comtypes\client\lazybind.py,第182行,在调用者返回self.\u comobj.\u invokedescr.memid,descr.invkind,0,*args文件C:\Users\nithish\AppData\Local\Programs\Python\Python37\lib\site packages\comtypes\automation.py,第729行,在调用dp,var,None,argerr\u ctypes.comer:-2147352567,“发生异常”,“AutoCAD”,无,0,现在没有我收到这个错误cwd当时的值是多少?D:\autocad_files@reckfaceFile C:\Users\nithish\AppData\Local\Programs\Python\Python37\lib\site packages\comtypes\automation.py,第729行,在(调用dp,var,None,argerr)ctypes.comeror:-2147352567,“发生异常”。“方法在MDI模式下不可用。”。使用文档集合的打开方法。“,”AutoCAD“,”C:\\Program Files\\Autodesk\\AutoCAD 2019\\HELP\\OLE_ERR.CHM“,-2145320848,None@NithishAlbin最后尝试一件事:acad.app.Documents.open