質問編集履歴
4
環境の変更とエラーメッセージの変化
test
CHANGED
File without changes
|
test
CHANGED
@@ -248,3 +248,15 @@
|
|
248
248
|
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\ntpath.py", line 104, in join
|
249
249
|
path = os.fspath(path)
|
250
250
|
TypeError: expected str, bytes or os.PathLike object, not StringVar
|
251
|
+
|
252
|
+
8月5日追記
|
253
|
+
Pythonのバージョンを、3.10.6にしました。
|
254
|
+
また、エラーメッセージが変わりました。
|
255
|
+
変わった後のエラーメッセージは以下の通りです。
|
256
|
+
Exception in Tkinter callback
|
257
|
+
Traceback (most recent call last):
|
258
|
+
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
|
259
|
+
return self.func(*args)
|
260
|
+
File "c:\Users\hirom\Desktop\pdf_image_extract\program.py", line 94, in <lambda>
|
261
|
+
stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked())
|
262
|
+
TypeError: start_clicked() missing 1 required positional argument: 'fp'
|
3
try文をやめた後のコードとエラー文を記載しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -127,3 +127,124 @@
|
|
127
127
|
エラー内容は以下の通りです。
|
128
128
|
sysモジュールにてsys.exc_info()で取得
|
129
129
|
(<class 'TypeError'>, TypeError('expected str, bytes or os.PathLike object, not StringVar'), <traceback object at 0x0000025AD194BC40>)
|
130
|
+
|
131
|
+
8月4日追記2
|
132
|
+
try文をやめてみたところ、エラー箇所がわかりました。
|
133
|
+
エラー箇所は、42行目と94行目でした。
|
134
|
+
ソースコードと、エラー文を貼っておきます。
|
135
|
+
コード
|
136
|
+
```Python
|
137
|
+
# インポート
|
138
|
+
import sys, os, fitz
|
139
|
+
from tkinter import Tk, StringVar,ttk,filedialog,messagebox,LEFT
|
140
|
+
|
141
|
+
# 参照ボタンクリック時
|
142
|
+
def ref_clicked():
|
143
|
+
fTyp = [("PDFファイル",".pdf")]
|
144
|
+
iDir = os.path.abspath(os.path.dirname(__file__))
|
145
|
+
global read_path
|
146
|
+
read_file = filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir)
|
147
|
+
file1.set(read_file)
|
148
|
+
|
149
|
+
def ref_clicked2():
|
150
|
+
iDir = os.path.abspath(os.path.dirname(__file__))
|
151
|
+
global save_path
|
152
|
+
save_path = filedialog.askdirectory(initialdir=iDir)
|
153
|
+
file2.set(save_path)
|
154
|
+
|
155
|
+
|
156
|
+
# スタートボタンクリック時
|
157
|
+
def start_clicked(fp):
|
158
|
+
if fp:
|
159
|
+
doc = fitz.open(file1.get())
|
160
|
+
images = []
|
161
|
+
for page in range(len(doc)):
|
162
|
+
images.append(doc[page].get_images())
|
163
|
+
for pageNo, image in enumerate(images):
|
164
|
+
if image != []:
|
165
|
+
for i in range(len(image)):
|
166
|
+
xref = image[i][0]
|
167
|
+
smask = image[i][1]
|
168
|
+
if image[i][8] == 'FlateDecode':
|
169
|
+
ext = 'png'
|
170
|
+
elif image[i][8] == 'DCTDecode':
|
171
|
+
ext = 'jpeg'
|
172
|
+
pix = fitz.Pixmap(doc.extract_image(xref)["image"])
|
173
|
+
if smask > 0:
|
174
|
+
mask = fitz.Pixmap(doc.extract_image(smask)["image"])
|
175
|
+
pix = fitz.Pixmap(pix, 0)
|
176
|
+
pix = fitz.Pixmap(pix, mask)
|
177
|
+
|
178
|
+
img_name = os.path.join(file2, f'image{pageNo+1}{i}.{ext}')
|
179
|
+
pix.save(img_name)
|
180
|
+
# 完了メッセージを表示
|
181
|
+
messagebox.showinfo("完了", "処理が完了しました。")
|
182
|
+
### GUI ###
|
183
|
+
if __name__ == '__main__':
|
184
|
+
# ウィンドウの設定
|
185
|
+
root = Tk()
|
186
|
+
root.title("画像の抽出を行いたいPDFファイルを選択してください。")
|
187
|
+
root.resizable(False, False)
|
188
|
+
|
189
|
+
# Frame1の作成
|
190
|
+
frame1 = ttk.Frame(root, padding=10)
|
191
|
+
frame1.grid(row=0)
|
192
|
+
|
193
|
+
# ファイルラベルの作成
|
194
|
+
s_file = StringVar()
|
195
|
+
label1 = ttk.Label(frame1, textvariable='PDFファイル')
|
196
|
+
label1.grid(row=0, column=1)
|
197
|
+
|
198
|
+
# 参照パスラベルの作成
|
199
|
+
file1 = StringVar()
|
200
|
+
file1_entry = ttk.Entry(frame1, textvariable=file1, width=50)
|
201
|
+
file1_entry.grid(row=0, column=2)
|
202
|
+
|
203
|
+
# 参照ボタンの作成
|
204
|
+
button1 = ttk.Button(root, text=u'参照', command=lambda: ref_clicked())
|
205
|
+
button1.grid(row=0, column=3)
|
206
|
+
|
207
|
+
# frame2の作成
|
208
|
+
frame2 = ttk.Frame(root, padding=(0, 5))
|
209
|
+
frame2.grid(row=1)
|
210
|
+
|
211
|
+
# ディレクトリ選択スペースの作成
|
212
|
+
s_dir = StringVar()
|
213
|
+
label2 = ttk.Label(frame2, text='保存場所')
|
214
|
+
label2.grid(row=0, column=1)
|
215
|
+
file2 = StringVar()
|
216
|
+
global entry2
|
217
|
+
entry2 = ttk.Entry(frame2, textvariable=file2, width=50)
|
218
|
+
entry2.grid(row=0, column=2)
|
219
|
+
|
220
|
+
# 参照ボタンの作成
|
221
|
+
refbutton = ttk.Button(frame2, text='参照', command=lambda: ref_clicked2())
|
222
|
+
refbutton.grid(row=0, column=3)
|
223
|
+
|
224
|
+
#frame3の作成
|
225
|
+
frame3 = ttk.Frame(root, padding=10)
|
226
|
+
frame3.grid(row=2)
|
227
|
+
|
228
|
+
|
229
|
+
# Startボタンの作成
|
230
|
+
stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked())
|
231
|
+
stButton.pack(side=LEFT)
|
232
|
+
|
233
|
+
# Cancelボタンの作成
|
234
|
+
button3 = ttk.Button(frame3, text='キャンセル', command=lambda: sys.exit())
|
235
|
+
button3.pack(side=LEFT)
|
236
|
+
|
237
|
+
root.mainloop()
|
238
|
+
```
|
239
|
+
エラーメッセージ
|
240
|
+
Exception in Tkinter callback
|
241
|
+
Traceback (most recent call last):
|
242
|
+
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
|
243
|
+
return self.func(*args)
|
244
|
+
File "C:\Users\hirom\Desktop\pdf_image_extract\program.py", line 94, in <lambda>
|
245
|
+
stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked())
|
246
|
+
File "C:\Users\hirom\Desktop\pdf_image_extract\program.py", line 42, in start_clicked
|
247
|
+
img_name = os.path.join(file2, f'image{pageNo+1}{i}.{ext}')
|
248
|
+
File "C:\Users\hirom\AppData\Local\Programs\Python\Python310\lib\ntpath.py", line 104, in join
|
249
|
+
path = os.fspath(path)
|
250
|
+
TypeError: expected str, bytes or os.PathLike object, not StringVar
|
2
エラー内容追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -122,3 +122,8 @@
|
|
122
122
|
|
123
123
|
root.mainloop()
|
124
124
|
```
|
125
|
+
|
126
|
+
8月4日追記
|
127
|
+
エラー内容は以下の通りです。
|
128
|
+
sysモジュールにてsys.exc_info()で取得
|
129
|
+
(<class 'TypeError'>, TypeError('expected str, bytes or os.PathLike object, not StringVar'), <traceback object at 0x0000025AD194BC40>)
|
1
参考サイトとコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,3 +8,117 @@
|
|
8
8
|
Windows 10 Home 64ビット
|
9
9
|
Python 3.10.5
|
10
10
|
PyMuPDF 1.20.1
|
11
|
+
|
12
|
+
追記
|
13
|
+
処理はこのサイトを参考にしました。
|
14
|
+
https://python-work.com/pdf-get-image/
|
15
|
+
|
16
|
+
コードは以下の通りです。
|
17
|
+
```Python
|
18
|
+
# インポート
|
19
|
+
import sys, os, fitz
|
20
|
+
from tkinter import Tk, StringVar,ttk,filedialog,messagebox,LEFT
|
21
|
+
|
22
|
+
# 参照ボタンクリック時
|
23
|
+
def ref_clicked():
|
24
|
+
fTyp = [("PDFファイル",".pdf")]
|
25
|
+
iDir = os.path.abspath(os.path.dirname(__file__))
|
26
|
+
global read_path
|
27
|
+
read_file = filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir)
|
28
|
+
file1.set(read_file)
|
29
|
+
|
30
|
+
def ref_clicked2():
|
31
|
+
iDir = os.path.abspath(os.path.dirname(__file__))
|
32
|
+
global save_path
|
33
|
+
save_path = filedialog.askdirectory(initialdir=iDir)
|
34
|
+
file2.set(save_path)
|
35
|
+
|
36
|
+
|
37
|
+
# スタートボタンクリック時
|
38
|
+
def start_clicked(fp):
|
39
|
+
try:
|
40
|
+
if fp:
|
41
|
+
doc = fitz.open(file1.get())
|
42
|
+
images = []
|
43
|
+
for page in range(len(doc)):
|
44
|
+
images.append(doc[page].get_images())
|
45
|
+
for pageNo, image in enumerate(images):
|
46
|
+
if image != []:
|
47
|
+
for i in range(len(image)):
|
48
|
+
xref = image[i][0]
|
49
|
+
smask = image[i][1]
|
50
|
+
if image[i][8] == 'FlateDecode':
|
51
|
+
ext = 'png'
|
52
|
+
elif image[i][8] == 'DCTDecode':
|
53
|
+
ext = 'jpeg'
|
54
|
+
pix = fitz.Pixmap(doc.extract_image(xref)["image"])
|
55
|
+
if smask > 0:
|
56
|
+
mask = fitz.Pixmap(doc.extract_image(smask)["image"])
|
57
|
+
pix = fitz.Pixmap(pix, 0)
|
58
|
+
pix = fitz.Pixmap(pix, mask)
|
59
|
+
|
60
|
+
img_name = os.path.join(file2, f'image{pageNo+1}{i}.{ext}')
|
61
|
+
pix.save(img_name)
|
62
|
+
# 完了メッセージを表示
|
63
|
+
messagebox.showinfo("完了", "処理が完了しました。")
|
64
|
+
except:
|
65
|
+
messagebox.showerror("エラーが発生しました。", sys.exc_info())
|
66
|
+
print(sys.exc_info()) # デバッグ用
|
67
|
+
|
68
|
+
### GUI ###
|
69
|
+
if __name__ == '__main__':
|
70
|
+
# ウィンドウの設定
|
71
|
+
root = Tk()
|
72
|
+
root.title("画像の抽出を行いたいPDFファイルを選択してください。")
|
73
|
+
root.resizable(False, False)
|
74
|
+
|
75
|
+
# Frame1の作成
|
76
|
+
frame1 = ttk.Frame(root, padding=10)
|
77
|
+
frame1.grid(row=0)
|
78
|
+
|
79
|
+
# ファイルラベルの作成
|
80
|
+
s_file = StringVar()
|
81
|
+
label1 = ttk.Label(frame1, textvariable='PDFファイル')
|
82
|
+
label1.grid(row=0, column=1)
|
83
|
+
|
84
|
+
# 参照パスラベルの作成
|
85
|
+
file1 = StringVar()
|
86
|
+
file1_entry = ttk.Entry(frame1, textvariable=file1, width=50)
|
87
|
+
file1_entry.grid(row=0, column=2)
|
88
|
+
|
89
|
+
# 参照ボタンの作成
|
90
|
+
button1 = ttk.Button(root, text=u'参照', command=lambda: ref_clicked())
|
91
|
+
button1.grid(row=0, column=3)
|
92
|
+
|
93
|
+
# frame2の作成
|
94
|
+
frame2 = ttk.Frame(root, padding=(0, 5))
|
95
|
+
frame2.grid(row=1)
|
96
|
+
|
97
|
+
# ディレクトリ選択スペースの作成
|
98
|
+
s_dir = StringVar()
|
99
|
+
label2 = ttk.Label(frame2, text='保存場所')
|
100
|
+
label2.grid(row=0, column=1)
|
101
|
+
file2 = StringVar()
|
102
|
+
global entry2
|
103
|
+
entry2 = ttk.Entry(frame2, textvariable=file2, width=50)
|
104
|
+
entry2.grid(row=0, column=2)
|
105
|
+
|
106
|
+
# 参照ボタンの作成
|
107
|
+
refbutton = ttk.Button(frame2, text='参照', command=lambda: ref_clicked2())
|
108
|
+
refbutton.grid(row=0, column=3)
|
109
|
+
|
110
|
+
#frame3の作成
|
111
|
+
frame3 = ttk.Frame(root, padding=10)
|
112
|
+
frame3.grid(row=2)
|
113
|
+
|
114
|
+
|
115
|
+
# Startボタンの作成
|
116
|
+
stButton = ttk.Button(frame3, text='実行', command=lambda: start_clicked(file1.get()))
|
117
|
+
stButton.pack(side=LEFT)
|
118
|
+
|
119
|
+
# Cancelボタンの作成
|
120
|
+
button3 = ttk.Button(frame3, text='キャンセル', command=lambda: sys.exit())
|
121
|
+
button3.pack(side=LEFT)
|
122
|
+
|
123
|
+
root.mainloop()
|
124
|
+
```
|