PythonでPDFから画像を抽出するプログラムを作成しています。
GUIで作成しています。
tkinterとPyMuPDFを使っています。
PyMuPDFでファイルを開く処理の時に、GUIから取得したファイル名で行いたいのですが、実装中にエラーが出ています。
どうすればエラーにならずにGUIからファイル名を取得して処理を行うことができるのか教えてください。
開発環境は以下の通りです。
Windows 10 Home 64ビット
Python 3.10.5
PyMuPDF 1.20.1
追記
処理はこのサイトを参考にしました。
https://python-work.com/pdf-get-image/
コードは以下の通りです。
Python
1# インポート 2import sys, os, fitz 3from tkinter import Tk, StringVar,ttk,filedialog,messagebox,LEFT 4 5# 参照ボタンクリック時 6def ref_clicked(): 7 fTyp = [("PDFファイル",".pdf")] 8 iDir = os.path.abspath(os.path.dirname(__file__)) 9 global read_path 10 read_file = filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) 11 file1.set(read_file) 12 13def ref_clicked2(): 14 iDir = os.path.abspath(os.path.dirname(__file__)) 15 global save_path 16 save_path = filedialog.askdirectory(initialdir=iDir) 17 file2.set(save_path) 18 19 20# スタートボタンクリック時 21def start_clicked(fp): 22 try: 23 if fp: 24 doc = fitz.open(file1.get()) 25 images = [] 26 for page in range(len(doc)): 27 images.append(doc[page].get_images()) 28 for pageNo, image in enumerate(images): 29 if image != []: 30 for i in range(len(image)): 31 xref = image[i][0] 32 smask = image[i][1] 33 if image[i][8] == 'FlateDecode': 34 ext = 'png' 35 elif image[i][8] == 'DCTDecode': 36 ext = 'jpeg' 37 pix = fitz.Pixmap(doc.extract_image(xref)["image"]) 38 if smask > 0: 39 mask = fitz.Pixmap(doc.extract_image(smask)["image"]) 40 pix = fitz.Pixmap(pix, 0) 41 pix = fitz.Pixmap(pix, mask) 42 43 img_name = os.path.join(file2, f'image{pageNo+1}{i}.{ext}') 44 pix.save(img_name) 45 # 完了メッセージを表示 46 messagebox.showinfo("完了", "処理が完了しました。") 47 except: 48 messagebox.showerror("エラーが発生しました。", sys.exc_info()) 49 print(sys.exc_info()) # デバッグ用 50 51### GUI ### 52if __name__ == '__main__': 53 # ウィンドウの設定 54 root = Tk() 55 root.title("画像の抽出を行いたいPDFファイルを選択してください。") 56 root.resizable(False, False) 57 58 # Frame1の作成 59 frame1 = ttk.Frame(root, padding=10) 60 frame1.grid(row=0) 61 62 # ファイルラベルの作成 63 s_file = StringVar() 64 label1 = ttk.Label(frame1, textvariable='PDFファイル') 65 label1.grid(row=0, column=1) 66 67 # 参照パスラベルの作成 68 file1 = StringVar() 69 file1_entry = ttk.Entry(frame1, textvariable=file1, width=50) 70 file1_entry.grid(row=0, column=2) 71 72 # 参照ボタンの作成 73 button1 = ttk.Button(root, text=u'参照', command=lambda: ref_clicked()) 74 button1.grid(row=0, column=3) 75 76 # frame2の作成 77 frame2 = ttk.Frame(root, padding=(0, 5)) 78 frame2.grid(row=1) 79 80 # ディレクトリ選択スペースの作成 81 s_dir = StringVar() 82 label2 = ttk.Label(frame2, text='保存場所') 83 label2.grid(row=0, column=1) 84 file2 = StringVar() 85 global entry2 86 entry2 = ttk.Entry(frame2, textvariable=file2, width=50) 87 entry2.grid(row=0, column=2) 88 89 # 参照ボタンの作成 90 refbutton = ttk.Button(frame2, text='参照', command=lambda: ref_clicked2()) 91 refbutton.grid(row=0, column=3) 92 93 #frame3の作成 94 frame3 = ttk.Frame(root, padding=10) 95 frame3.grid(row=2) 96 97 98 # Startボタンの作成 99 stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked(file1.get())) 100 stButton.pack(side=LEFT) 101 102 # Cancelボタンの作成 103 button3 = ttk.Button(frame3, text='キャンセル', command=lambda: sys.exit()) 104 button3.pack(side=LEFT) 105 106 root.mainloop()
8月4日追記
エラー内容は以下の通りです。
sysモジュールにてsys.exc_info()で取得
(<class 'TypeError'>, TypeError('expected str, bytes or os.PathLike object, not StringVar'), <traceback object at 0x0000025AD194BC40>)
8月4日追記2
try文をやめてみたところ、エラー箇所がわかりました。
エラー箇所は、42行目と94行目でした。
ソースコードと、エラー文を貼っておきます。
コード
Python
1# インポート 2import sys, os, fitz 3from tkinter import Tk, StringVar,ttk,filedialog,messagebox,LEFT 4 5# 参照ボタンクリック時 6def ref_clicked(): 7 fTyp = [("PDFファイル",".pdf")] 8 iDir = os.path.abspath(os.path.dirname(__file__)) 9 global read_path 10 read_file = filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) 11 file1.set(read_file) 12 13def ref_clicked2(): 14 iDir = os.path.abspath(os.path.dirname(__file__)) 15 global save_path 16 save_path = filedialog.askdirectory(initialdir=iDir) 17 file2.set(save_path) 18 19 20# スタートボタンクリック時 21def start_clicked(fp): 22 if fp: 23 doc = fitz.open(file1.get()) 24 images = [] 25 for page in range(len(doc)): 26 images.append(doc[page].get_images()) 27 for pageNo, image in enumerate(images): 28 if image != []: 29 for i in range(len(image)): 30 xref = image[i][0] 31 smask = image[i][1] 32 if image[i][8] == 'FlateDecode': 33 ext = 'png' 34 elif image[i][8] == 'DCTDecode': 35 ext = 'jpeg' 36 pix = fitz.Pixmap(doc.extract_image(xref)["image"]) 37 if smask > 0: 38 mask = fitz.Pixmap(doc.extract_image(smask)["image"]) 39 pix = fitz.Pixmap(pix, 0) 40 pix = fitz.Pixmap(pix, mask) 41 42 img_name = os.path.join(file2, f'image{pageNo+1}{i}.{ext}') 43 pix.save(img_name) 44 # 完了メッセージを表示 45 messagebox.showinfo("完了", "処理が完了しました。") 46### GUI ### 47if __name__ == '__main__': 48 # ウィンドウの設定 49 root = Tk() 50 root.title("画像の抽出を行いたいPDFファイルを選択してください。") 51 root.resizable(False, False) 52 53 # Frame1の作成 54 frame1 = ttk.Frame(root, padding=10) 55 frame1.grid(row=0) 56 57 # ファイルラベルの作成 58 s_file = StringVar() 59 label1 = ttk.Label(frame1, textvariable='PDFファイル') 60 label1.grid(row=0, column=1) 61 62 # 参照パスラベルの作成 63 file1 = StringVar() 64 file1_entry = ttk.Entry(frame1, textvariable=file1, width=50) 65 file1_entry.grid(row=0, column=2) 66 67 # 参照ボタンの作成 68 button1 = ttk.Button(root, text=u'参照', command=lambda: ref_clicked()) 69 button1.grid(row=0, column=3) 70 71 # frame2の作成 72 frame2 = ttk.Frame(root, padding=(0, 5)) 73 frame2.grid(row=1) 74 75 # ディレクトリ選択スペースの作成 76 s_dir = StringVar() 77 label2 = ttk.Label(frame2, text='保存場所') 78 label2.grid(row=0, column=1) 79 file2 = StringVar() 80 global entry2 81 entry2 = ttk.Entry(frame2, textvariable=file2, width=50) 82 entry2.grid(row=0, column=2) 83 84 # 参照ボタンの作成 85 refbutton = ttk.Button(frame2, text='参照', command=lambda: ref_clicked2()) 86 refbutton.grid(row=0, column=3) 87 88 #frame3の作成 89 frame3 = ttk.Frame(root, padding=10) 90 frame3.grid(row=2) 91 92 93 # Startボタンの作成 94 stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked()) 95 stButton.pack(side=LEFT) 96 97 # Cancelボタンの作成 98 button3 = ttk.Button(frame3, text='キャンセル', command=lambda: sys.exit()) 99 button3.pack(side=LEFT) 100 101 root.mainloop()
エラーメッセージ
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call
return self.func(*args)
File "C:\Users\hirom\Desktop\pdf_image_extract\program.py", line 94, in <lambda>
stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked())
File "C:\Users\hirom\Desktop\pdf_image_extract\program.py", line 42, in start_clicked
img_name = os.path.join(file2, f'image{pageNo+1}{i}.{ext}')
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\ntpath.py", line 104, in join
path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not StringVar
8月5日追記
Pythonのバージョンを、3.10.6にしました。
また、エラーメッセージが変わりました。
変わった後のエラーメッセージは以下の通りです。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call
return self.func(*args)
File "c:\Users\hirom\Desktop\pdf_image_extract\program.py", line 94, in <lambda>
stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked())
TypeError: start_clicked() missing 1 required positional argument: 'fp'

回答1件
あなたの回答
tips
プレビュー