Is pyautocad needs a running autoCad Software?

Question:

I am getting an error . when I try to convert dwg AutoCAD file to excel.
I used http://www.run8tech.com/script-to-update-text-and-mtext-in-autocad-from-excel.aspx
I am getting an error

this is the script I ran. Is autocad running is necessary for using this library 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")

when I run this script error I am getting is this

File "test.py", line 36, in <module>
  acad.doc.Open(cwd + "/" + f)
File "C:UsersnithishAppDataLocalProgramsPythonPython37libsite-packagespyautocadapi.py", line 74, in doc
  return self.app.ActiveDocument
File "C:UsersnithishAppDataLocalProgramsPythonPython37libsite-packagespyautocadapi.py", line 63, in app
  self._app = comtypes.client.GetActiveObject('AutoCAD.Application', dynamic=True)
File "C:UsersnithishAppDataLocalProgramsPythonPython37libsite-packagescomtypesclient__init__.py", line 173, in GetActiveObject
  clsid = comtypes.GUID.from_progid(progid)
File "C:UsersnithishAppDataLocalProgramsPythonPython37libsite-packagescomtypesGUID.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
Asked By: Nithish Albin

||

Answers:

You would get the error:

OSError: [WinError -2147221005] Invalid class string

If

  1. You don’t have AutoCAD installed
  2. If you have a version installed, but are trying to instantiate the wrong version like: CreateObject("Autocad19.Application") when you have AutoCAD20 installed.

In general, libraries leave the version number out, so “Autocad.Application” would work fine.

The call to Autocad() will start an instance of AutoCAD if it is installed.

Answered By: reckface

Here is my environment:

  1. AutoCAD: AutoCAD Civil 3D 2018
  2. Python: Python 3.8.2
  3. OS: Windows 10

I passed the create_if_not_exists argument of pyautocad.Autocad to True. Then the code started working without problem.
So try doing this:

acad = Autocad(create_if_not_exists=True)
Answered By: Shams E Shifat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.