convert プログラム仕様
- 概要:
各種ファイル形式の変換処理を統一的に管理・実行するスクリプトです。
- 詳細説明:
Word、Excel、PowerPoint、MarkdownなどのファイルをPDFなどの目的の形式に変換します。 変換器はレジストリに登録され、ディレクトリ内の再帰的な一括変換や単一ファイルの変換をサポートします。
- class converter.convert.ConverterRegistry
ベースクラス:
object- 概要:
変換器を管理するレジストリクラスです。
- 詳細説明:
入出力の拡張子に応じた適切な変換器を登録および取得する機能を提供します。
- get(input_ext: str, output_ext: str) ConverterSpec | None
- 概要:
指定された拡張子の組み合わせに対応する変換器を取得します。
- 引数:
- param input_ext:
入力ファイルの拡張子
- type input_ext:
str
- param output_ext:
出力ファイルの拡張子
- type output_ext:
str
- 戻り値:
- returns:
変換器の仕様オブジェクト、見つからない場合はNone
- rtype:
ConverterSpec
- has(input_ext: str, output_ext: str) bool
- 概要:
指定された拡張子の組み合わせに対応する変換器が存在するか判定します。
- 引数:
- param input_ext:
入力ファイルの拡張子
- type input_ext:
str
- param output_ext:
出力ファイルの拡張子
- type output_ext:
str
- 戻り値:
- returns:
変換器が存在する場合はTrue、それ以外はFalse
- rtype:
bool
- list_output_exts()
- 概要:
登録されているすべての出力拡張子をソートされたリストとして返します。
- 戻り値:
- returns:
出力拡張子のリスト
- rtype:
list
- list_specs()
- 概要:
登録されているすべての変換器の仕様をリストとして返します。
- 戻り値:
- returns:
変換器仕様オブジェクトのリスト
- rtype:
list
- print_available_converters()
- 概要:
利用可能な変換器の一覧と、利用できないモジュールを標準出力に表示します。
- register(spec: ConverterSpec)
- 概要:
変換器の仕様をレジストリに登録します。
- 引数:
- param spec:
登録する変換器の仕様
- type spec:
ConverterSpec
- class converter.convert.ConverterSpec(input_ext: str, output_ext: str, converter: ~typing.Callable[[...], ~typing.Any], description: str = '', ignore_temp_prefixes: ~typing.Tuple[str, ...] = (), output_mode: str = 'file', options: ~typing.Dict[str, ~typing.Any] = <factory>)
ベースクラス:
object- 概要:
変換器の仕様を定義するデータクラスです。
- 詳細説明:
入力と出力の拡張子、実行する変換関数、およびその他のメタデータを保持します。
- 引数:
- param input_ext:
入力ファイルの拡張子
- type input_ext:
str
- param output_ext:
出力ファイルの拡張子
- type output_ext:
str
- param converter:
変換処理を実行する関数
- type converter:
Callable
- param description:
変換器の説明文
- type description:
str
- param ignore_temp_prefixes:
無視する一時ファイルの接頭辞のタプル
- type ignore_temp_prefixes:
tuple
- param output_mode:
出力モードを示す文字列
- type output_mode:
str
- param options:
変換器に渡す追加オプションの辞書
- type options:
dict
- converter.convert.build_converter_kwargs(args, output_ext: str) Dict[str, Any]
- 概要:
コマンドライン引数から変換器に渡すキーワード引数を構築します。
- 引数:
- param args:
パース済みのコマンドライン引数
- type args:
argparse.Namespace
- param output_ext:
対象の出力拡張子
- type output_ext:
str
- 戻り値:
- returns:
変換器用の追加引数が格納された辞書
- rtype:
dict
- converter.convert.build_output_path(input_path: str, output_ext: str, output_mode: str = 'file') str
- 概要:
出力先のファイルまたはディレクトリパスを構築します。
- 詳細説明:
出力モードがディレクトリの場合は拡張子に s を付与した名前を返します。
- 引数:
- param input_path:
入力ファイルのパス
- type input_path:
str
- param output_ext:
対象の出力拡張子
- type output_ext:
str
- param output_mode:
出力モード
- type output_mode:
str
- 戻り値:
- returns:
構築された出力パス
- rtype:
str
- converter.convert.convert_file(input_path: str, output_ext: str, registry: ConverterRegistry, update: bool = True, overwrite: bool = False, converter_kwargs: Dict[str, Any] | None = None) bool
- 概要:
単一のファイルを変換します。
- 詳細説明:
指定されたファイルに対してレジストリから適切な変換器を取得し、変換を実行します。
- 引数:
- param input_path:
入力ファイルのパス
- type input_path:
str
- param output_ext:
対象の出力拡張子
- type output_ext:
str
- param registry:
変換器レジストリ
- type registry:
ConverterRegistry
- param update:
出力ファイルが存在し、入力より新しい場合はスキップするかのフラグ
- type update:
bool
- param overwrite:
常に変換を実行するかのフラグ
- type overwrite:
bool
- param converter_kwargs:
変換器に渡す追加引数
- type converter_kwargs:
dict
- 戻り値:
- returns:
変換に成功した場合はTrue、それ以外はFalse
- rtype:
bool
- converter.convert.get_latest_mtime(path: str, output_mode: str) float | None
- 概要:
指定されたパスの最新の更新日時を取得します。
- 詳細説明:
ディレクトリモードの場合は再帰的にファイルを走査し、最も新しい更新日時を返します。
- 引数:
- param path:
対象のパス
- type path:
str
- param output_mode:
出力モード
- type output_mode:
str
- 戻り値:
- returns:
最新の更新日時を表すタイムスタンプ、存在しない場合はNone
- rtype:
float
- converter.convert.main()
- 概要:
スクリプトのメイン処理を実行します。
- 詳細説明:
コマンドライン引数の解析、レジストリの初期化、指定されたモードに応じた変換処理を行います。
- 戻り値:
- returns:
終了コード
- rtype:
int
- converter.convert.matches_target(filename: str, target_patterns: List[str] | None) bool
- 概要:
ファイル名が対象パターンのいずれかに一致するか判定します。
- 引数:
- param filename:
判定するファイル名
- type filename:
str
- param target_patterns:
対象パターンのリスト
- type target_patterns:
list
- 戻り値:
- returns:
一致する場合はTrue、それ以外はFalse
- rtype:
bool
- converter.convert.normalize_ext(ext: str) str
- 概要:
拡張子を正規化します。
- 詳細説明:
小文字に変換し、先頭にピリオドがない場合は付与します。
- 引数:
- param ext:
元の拡張子
- type ext:
str
- 戻り値:
- returns:
正規化された拡張子
- rtype:
str
- converter.convert.parse_args()
- 概要:
コマンドライン引数をパースします。
- 戻り値:
- returns:
パース結果を格納した名前空間オブジェクト
- rtype:
argparse.Namespace
- converter.convert.parse_target_patterns(target_text: str | None) List[str] | None
- 概要:
セミコロン区切りの対象ファイルパターンの文字列をリストにパースします。
- 引数:
- param target_text:
対象パターンの文字列
- type target_text:
str
- 戻り値:
- returns:
パースされたパターンのリスト、入力が空の場合はNone
- rtype:
list
- converter.convert.path_exists_for_mode(path: str, output_mode: str) bool
- 概要:
指定された出力モードにおいてパスが有効に存在するかを確認します。
- 詳細説明:
ディレクトリモードの場合はディレクトリが存在し、かつ空でないことを確認します。
- 引数:
- param path:
確認するパス
- type path:
str
- param output_mode:
出力モード
- type output_mode:
str
- 戻り値:
- returns:
パスが存在し条件を満たす場合はTrue、それ以外はFalse
- rtype:
bool
- converter.convert.safe_import_registry(registry: ConverterRegistry)
- 概要:
利用可能な各種モジュールを安全にインポートし、レジストリに登録します。
- 詳細説明:
インポートに失敗した場合はエラー情報をレジストリに記録し、処理を続行します。
- 引数:
- param registry:
登録先のレジストリインスタンス
- type registry:
ConverterRegistry
- converter.convert.should_convert(input_path: str, output_path: str, output_mode: str, update: bool, overwrite: bool) Tuple[bool, str]
- 概要:
変換を実行すべきかどうかとその理由を判定します。
- 詳細説明:
上書き設定やファイルの更新日時を比較して、変換の要否を決定します。
- 引数:
- param input_path:
入力ファイルのパス
- type input_path:
str
- param output_path:
出力ファイルのパス
- type output_path:
str
- param output_mode:
出力モード
- type output_mode:
str
- param update:
更新のみ行うかどうかのフラグ
- type update:
bool
- param overwrite:
強制的に上書きするかどうかのフラグ
- type overwrite:
bool
- 戻り値:
- returns:
変換実行の要否と判定理由の文字列のタプル
- rtype:
tuple
- converter.convert.should_ignore_file(filename: str, spec: ConverterSpec) bool
- 概要:
ファイルが一時ファイルとして無視すべき対象かどうかを判定します。
- 引数:
- param filename:
判定するファイル名
- type filename:
str
- param spec:
適用する変換器の仕様
- type spec:
ConverterSpec
- 戻り値:
- returns:
無視すべきファイルの場合はTrue、それ以外はFalse
- rtype:
bool
- converter.convert.walk_and_convert(root_dir: str, output_ext: str, registry: ConverterRegistry, max_level: int = -1, update: bool = True, overwrite: bool = False, target_patterns: List[str] | None = None, converter_kwargs: Dict[str, Any] | None = None)
- 概要:
ディレクトリツリーを走査し、条件に一致するファイルを一括で変換します。
- 詳細説明:
再帰的にディレクトリを辿り、指定された対象パターンや更新条件を満たすファイルを変換します。
- 引数:
- param root_dir:
走査を開始するルートディレクトリ
- type root_dir:
str
- param output_ext:
対象の出力拡張子
- type output_ext:
str
- param registry:
変換器レジストリ
- type registry:
ConverterRegistry
- param max_level:
最大の再帰深度であり、-1の場合は制限なし
- type max_level:
int
- param update:
更新対象のみ変換するかのフラグ
- type update:
bool
- param overwrite:
常に上書き変換するかのフラグ
- type overwrite:
bool
- param target_patterns:
変換対象とするファイル名のパターンのリスト
- type target_patterns:
list
- param converter_kwargs:
変換器に渡す追加引数
- type converter_kwargs:
dict
- 戻り値:
- returns:
走査した対象ファイル数と変換されたファイル数のタプル
- rtype:
tuple
- converter.convert.wrap_docx_to_png(docx_to_pdf: Callable[[str, str], Any], pdf_to_images: Callable[[...], Any]) Callable[[...], bool]
- 概要:
WordファイルをPDF経由でPNG画像のディレクトリに変換する処理をラップします。
- 引数:
- param docx_to_pdf:
WordからPDFへの変換関数
- type docx_to_pdf:
Callable
- param pdf_to_images:
PDFから画像への変換関数
- type pdf_to_images:
Callable
- 戻り値:
- returns:
ラップされた関数
- rtype:
Callable
- converter.convert.wrap_image_to_dir(converter: Callable[[...], Any], rename_func: Callable[[str], Any] | None = None) Callable[[...], bool]
- 概要:
画像をディレクトリに出力する変換関数をラップします。
- 引数:
- param converter:
元の変換関数
- type converter:
Callable
- param rename_func:
出力後のリネーム処理を行う関数
- type rename_func:
Callable
- 戻り値:
- returns:
ラップされた関数
- rtype:
Callable
- converter.convert.wrap_ipynb_json_to_md(convert_func: Callable[[...], Any]) Callable[[...], bool]
- 概要:
Jupyter NotebookまたはJSONファイルからMarkdownへの変換関数をラップします。
- 引数:
- param convert_func:
元の変換関数
- type convert_func:
Callable
- 戻り値:
- returns:
ラップされた関数
- rtype:
Callable
- converter.convert.wrap_ipynb_json_to_pdf(ipynb_or_json_to_md: Callable[[...], Any], md_to_pdf: Callable[[...], Any]) Callable[[...], bool]
- 概要:
Jupyter NotebookまたはJSONファイルをMarkdown経由でPDFに変換する関数をラップします。
- 詳細説明:
一時ディレクトリを作成し、Markdownへ変換したのちPDFへの変換を実行します。
- 引数:
- param ipynb_or_json_to_md:
Markdownへの変換関数
- type ipynb_or_json_to_md:
Callable
- param md_to_pdf:
MarkdownからPDFへの変換関数
- type md_to_pdf:
Callable
- 戻り値:
- returns:
ラップされた関数
- rtype:
Callable
- converter.convert.wrap_md_with_images(convert_func: Callable[[...], Any]) Callable[[...], bool]
- 概要:
画像ディレクトリを伴うMarkdown変換関数をラップします。
- 引数:
- param convert_func:
元の変換関数
- type convert_func:
Callable
- 戻り値:
- returns:
ラップされた関数
- rtype:
Callable
- converter.convert.wrap_pandoc(target: str, default_template: str | None = None) Callable[[...], bool]
- 概要:
Pandocを使用した変換関数をラップします。
- 引数:
- param target:
変換先のフォーマット指定
- type target:
str
- param default_template:
デフォルトのテンプレートパス
- type default_template:
str
- 戻り値:
- returns:
ラップされた関数
- rtype:
Callable