如何使用python将autocad的dwg文件转换为png格式,有什么办法吗?

有什么方法可以使用python将dwg文件AutoCAD更改为png格式或pdf格式。


我尝试查看许多文档,但没有一个回答我的问题


```

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

from comtypes import COMError

def props(cls):

  return [i for i in cls.__dict__.keys() if i[:1] != '_']

# 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

print(cwd)



for f in dwgfiles:

    print("++++++++++++++++++++++++++++++")

    print("++++++++++++++++++++++++++++++")

    print("++++++++++++++++++++++++++++++")

    print("++++++++++++++++++++++++++++++")


    print(f)

    if f.endswith(".dwg"):

        print("sdaasdas")

        """ open Document"""

        acad = Autocad()

        print(cwd)

        acad.app.Documents.open(cwd + "/" + f)

        exportFile="new2.bmp"






        num_cols = xl_sheet.ncols  # Number of columns

        idx = 1


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


        doc = acad.ActiveDocument  # Document object

        print(dir(doc))

        doc.Export('exportFile','bmp')

        print("MODEL SPACE")

        count=0

```

请帮我解决这个问题?我得到的错误如下


**e_to_string_', '_username_', '_wrap_dispatch_']

Traceback (most recent call last):

  File "auto1.py", line 63, in <module>

    doc.Export(exportFile,"bmp")

  File "<COMObject <unknown>>", line 3, in Export

pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)**


largeQ
浏览 723回答 2
2回答

ITMISS

我认为最好的方法是运行 command _-PLOT,并使用虚拟 PDF 绘图仪。 您可以在这里找到更多信息。运行命令你可以使用:&nbsp; acad.doc.SendCommand("_-PLOT" paramerets )其中paramerets定义纸张大小,绘图区域等。我在 python 中没有任何示例,只有 LISP。我希望你能处理翻译。所以最简单的样本可能是:(setq parameters (list "_n" layoutName "" "PDFCreator" "_n" "_n" "_y")凡layoutName在我的情况下,变量layoutName被读取BE迭代每个布局。在这种情况下,所有选项都将是默认的 - 您需要在绘图窗口中初始化配置并使用按钮[Apply to Layout]。更复杂的例子可能是:(setq parameters (list&nbsp;"_y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; detailed configuration"Layout1"&nbsp; &nbsp; &nbsp; &nbsp;; Layout name"PDFCreator"&nbsp; &nbsp; ; ploter name&nbsp;"A4"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; page size""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; drawing units""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; drawing orientation""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; plot upside-down"_w"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; plot area - window(strcat (rtos(car P1)) "," (rtos(cadr P1)))&nbsp; &nbsp; &nbsp;; P1 and P2 are points ( three elemenets list of coordinates as real value ))(strcat (rtos(car P2)) "," (rtos(cadr P2)))&nbsp; &nbsp; &nbsp;; we need to convert real values to strings""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; drawing scale""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; plot offset""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; use plot style table""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; plot styme name""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Lineweight""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Lineweight scale""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; plot paperspace first""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; plot paperspace objects""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; save to file ( *.plt)"_n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; save changes to page configuration"_y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; confirm)

holdtom

如果您想导出为 PDF 或 PNG,这是不同的。对于 PDF,在我看来,最好的方法是使用虚拟绘图仪,例如 PDFCreator。因为您需要设置纸张大小等等。但是对于.PNG您可以使用 command&nbsp;_Export。默认它会显示窗口保存对话框,但如果您将系统变量设置FILEDIA为0,对话框将被禁用,您可以仅使用命令行导出。请记住最后将 FILEDIA 设置为旧值。在另一种情况下,它会让你非常不高兴;)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python