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"
)
任何洞察力或更正将不胜感激。谢谢!
我不知道我是否缺少任何东西,但是
SetCustomScale(1, 0.2)
SetCustomScale(1, 200)
或
SetCustomScale(1, 0.005)