我正在尝试使用 Python 在活动工作区中的多个物理模型上自动创建 powerdesigner 数据模型报告 - 更多地使用专门的 win32com 模块。我对 Python 还很陌生,所以一边尝试一边解决它,但遇到了困难。
我正在使用下面的代码(通过 GPT 重构以进行格式化),但在第 15 行出现属性错误,所以我猜测工作区没有名为“模型”的属性。一直在到处寻找与此相关的文档,但没有在任何地方找到它。
有人遇到过这个或有什么想法吗?
属性错误:.模型
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.")
如果将来有人在寻找这个问题,我的解决方法是使用generateRTF()函数。下面的代码是更广泛的解决方案。
''' #导出所有模型的报告并保存在主位置并在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}")
'''