[Python] AutoCAD win32com PDF Export - Custom Scale Issue

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to convert a DWG file to a PDF using win32com.client in Python, while applying a custom scale of 0.2 . However, the resulting PDF does not have the expected scale.

    Here is the code I'm using:

    import win32com.client
    import time
    import os

    def dwg_to_pdf_win32(dwg_file_path, pdf_file_path):
    try:
    # Initialize AutoCAD
    autocad = win32com.client.Dispatch("AutoCAD.Application")
    autocad.Visible = False

    # Open the DWG file
    doc = autocad.Documents.Open(dwg_file_path)
    layout = doc.Layouts.Item("Model")

    layout.PlotType = 2 # Extents
    layout.CenterPlot = True
    layout.PlotRotation = 1
    layout.ConfigName = "Microsoft Print to PDF"

    layout.CanonicalMediaName = "A3"
    layout.UseStandardScale = False
    layout.PaperUnits = 1
    layout.SetCustomScale(1, 0.2) # Scale: 1/200

    layout.PlotWithLineweights = False
    layout.PlotWithPlotStyles = True
    layout.StyleSheet = "style.ctb"

    # Create the PDF
    plot = doc.Plot
    plot.PlotToFile(pdf_file_path)

    # Wait for the PDF to be created
    timeout = 120
    for _ in range(timeout):
    if os.path.exists(pdf_file_path):
    print(f"PDF generated: {pdf_file_path}")
    break
    time.sleep(1)
    else:
    print("PDF file was not created.")
    return None

    # Close document
    doc.Close(False)
    print("Conversion successful!")

    except Exception as e:
    print(f"Error during conversion: {e}")

    # Example usage
    dwg_to_pdf_win32(
    "C:\\Users\\hp\\P8818-PLAN.dwg",
    "C:\\Users\\hp\\P8818-PLAN.pdf"
    )


    Any insights or corrections would be greatly appreciated. Thank you!

    Continue reading...

Compartilhe esta Página