0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

要件定義をPythonで図示する

Posted at

要件定義(PlantUML)ファイルを画像ファイルとして出力するPythonスクリプト例

以下のPythonコードをVSCode(拡張機能と設定要)にて実行すると,
カレントディレクトリ内にあるauto_forklift.puml(要件定義)から, AutonomousForkliftSystem.png という画像ファイルが出力される.

ここでは,生成AIを用いて,自律型フォークリフトの要件定義を作成してもらったものを使用することとした.

show_uml.py
import plantuml
import os

def render_puml_to_png(puml_file_path, output_png_path):
    """
    指定された.pumlファイルを読み込み、PNG画像として出力する関数。

    Args:
        puml_file_path (str): 入力となる.pumlファイルのパス。
        output_png_path (str): 出力するPNGファイルのパス。
    """
    try:
        # pumlファイルの内容を読み込む
        with open(puml_file_path, 'r', encoding='utf-8') as f:
            puml_code = f.read()

        # PlantUMLのオブジェクトを作成
        # ローカルにplantuml.jarがある場合は、jar_pathを指定
        # 例: p = plantuml.PlantUML(jar_path='/path/to/plantuml.jar')
        # サーバーを使う場合は、server_urlを指定
        p = plantuml.PlantUML(url='https://www.plantuml.com/plantuml/img/')
        
        # PNG画像を生成
        image_data = p.processes(puml_code)
        
        # ファイルとして保存
        with open(output_png_path, 'wb') as f:
            f.write(image_data)
            
        print(f"✅ PNG画像を生成しました: {output_png_path}")

    except FileNotFoundError:
        print(f"❌ エラー: ファイルが見つかりません - {puml_file_path}")
    except plantuml.PlantUMLServerError as e:
        print(f"❌ PlantUMLサーバーエラー: {e}")
    except Exception as e:
        print(f"❌ その他のエラーが発生しました: {e}")

# --- 使用例 ---
if __name__ == '__main__':
    # カレントディレクトリに'auto_forklift.puml'を作成
    puml_content = """
@startuml
class User {
    + name: string
    + email: string
}
class Order {
    + orderId: int
    + date: Date
}
User "1" -- "0..*" Order
@enduml
    """
    with open('auto_forklift.puml', 'w', encoding='utf-8') as f:
        f.write(puml_content)
        
    # スクリプトを実行して画像を生成
    input_file = 'auto_forklift.puml'
    output_file = 'AutonomousForkliftSystem.png'
    render_puml_to_png(input_file, output_file)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?