Darren Daly
Darren Daly

Reputation: 43

Python script to generate powerdesigner reports for all open models within a workspace

I am trying to automate the creation of powerdesigner data model reports on multiple physical models in an active workspace using Python- more using the specifically the win32com module. I'm fairly new to Python so trying to figure it out as I go but have hit a brick wall.

I am using the below code (Refactored through GPT for formatting) but am getting an attribute error on line 15 so I'm guessing workspace doesn't have an attribute called Models. Have been looking everywhere for relevant documentation around this but haven't found it anywhere.

Anyone come across this or have any ideas?

AttributeError: .Models

import win32com.client
import os
from pathlib import Path

# Connect to PowerDesigner
pd = win32com.client.Dispatch("PowerDesigner.Application")

# Get the active workspace
workspace = pd.ActiveWorkspace

# Get the Downloads folder path
downloads_folder = str(Path.home() / "Downloads")

# Iterate through open models in the workspace
for model in workspace.Models:
    print(f"Processing model: {model.Name}")
    
    # Iterate through reports in the model
    for report in model.Reports:
        if "model report" in report.Name.lower():
            # Generate a filename for the report
            filename = f"{model.Name}_{report.Name}.rtf"
            filepath = os.path.join(downloads_folder, filename)
            
            print(f"Saving report: {filename}")
            
            # Save the report
            try:
                report.Export(filepath, "RTF")
                print(f"Report saved to: {filepath}")
            except Exception as e:
                print(f"Error saving report: {e}")
    
    # Close the model
    print(f"Closing model: {model.Name}")
    model.Close()

print("Process completed.")

Upvotes: 1

Views: 40

Answers (1)

Darren Daly
Darren Daly

Reputation: 43

In case anyone is looking for this in the future, how I solved it was to use the generateRTF() function. Below code presents a wider solution.

#Export reports on all models and save in main location and close the model on powerdesigner
    
# Connect to PowerDesigner
pd = win32com.client.Dispatch("PowerDesigner.Application")
    
# Get the Downloads folder path
downloads_folder = dest_folder
    
# Iterate through open models in the workspace
for model in pd.Models:
    print(f"Processing model: {model.Name}")
    
    # Iterate through reports in the model
    for report in model.Reports:
        if "model report" in report.Name.lower():
            # Generate a filename for the report
            filename = f"{model.Name}_{report.Name}.rtf"
            filepath = os.path.join(downloads_folder, filename)
                
            print(f"Generating report: {filename}")
                
            # Save the report
            try:
                # Attempt to use the correct method to generate the report
                if hasattr(report, 'GenerateRTF'):
                    report.GenerateRTF(filepath, 0)  # Assuming 0 is the correct format code for RTF
                else:
                    print("No suitable export method found.")
                print(f"Report generated to: {filepath}")
            except Exception as e:
                print(f"Error generating report: {e}")

Upvotes: 1

Related Questions