import os import time import sys import win32com.client def find_ppt_files(directory): """再帰的にPowerPointファイルを検索する関数""" ppt_files = [] for root, _, files in os.walk(directory): for file in files: if file.lower().endswith(('.ppt', '.pptx')): full_path = os.path.join(root, file) ppt_files.append(full_path) return ppt_files def convert_ppt_to_pdf(input_file): """PowerPointファイルをPDFに変換する関数""" ppt = None try: ppt = win32com.client.Dispatch("PowerPoint.Application") ppt.Visible = 1 # 入力ファイルと同じディレクトリにPDFを出力 input_dir = os.path.dirname(input_file) pdf_file = os.path.splitext(os.path.basename(input_file))[0] + '.pdf' pdf_file_path = os.path.join(input_dir, pdf_file) print(f"PDF出力先: {pdf_file_path}") # プレゼンテーションを開く presentation = ppt.Presentations.Open(os.path.abspath(input_file)) # PDFとして保存 presentation.SaveAs(os.path.abspath(pdf_file_path), 32) presentation.Close() return pdf_file_path except Exception as e: print(f"PowerPointの変換でエラーが発生しました: {e}") return None finally: if ppt: try: ppt.Quit() ppt = None time.sleep(1) except: pass def main(): # コマンドライン引数を確認 if len(sys.argv) > 1: input_file = sys.argv[1] print("オプションで指定されたファイル:", input_file) ppt_files = [input_file] else: # カレントディレクトリを取得 input_dir = os.getcwd() print(f"検索開始ディレクトリ: {input_dir}") # 再帰的にPowerPointファイルを検索 ppt_files = find_ppt_files(input_dir) if not ppt_files: print("PowerPointファイルが見つかりませんでした。") return total_files = len(ppt_files) print(f"\n変換対象ファイル数: {total_files}") # 各PowerPointファイルを処理 for i, input_file in enumerate(ppt_files, 1): try: print(f"\n処理中 ({i}/{total_files}): {input_file}") pdf_file = convert_ppt_to_pdf(input_file) if pdf_file: print(f"変換成功: {pdf_file}") else: print(f"変換失敗: {input_file}") except Exception as e: print(f"処理中にエラーが発生しました: {e}") continue if __name__ == "__main__": print("このプログラムはPowerPointファイルをPDFに変換します。") print("オプションでファイル名を指定することで、そのファイルだけを変換します。") print("例: python app.py input.pptx") print("オプションを省略した場合、現在のディレクトリからすべてのPowerPointファイルを検索して変換します。") main()