質問編集履歴

1

コードの記載方法を修正しました。

2022/10/25 00:15

投稿

Py_komatta_123
Py_komatta_123

スコア0

test CHANGED
File without changes
test CHANGED
@@ -17,7 +17,7 @@
17
17
  ※Jpeg化するにあたり、Popplerを環境変数に設定して実行しています。
18
18
 
19
19
  ### 該当のソースコード
20
-
20
+ ```ここに言語を入力
21
21
  from pathlib import Path
22
22
  from pdf2image import convert_from_path
23
23
  import os
@@ -31,10 +31,17 @@
31
31
  from tkinter import filedialog
32
32
  import openpyxl as xl
33
33
 
34
+ #Excel to PDF
34
35
  def excel_PDF():
35
36
 
36
37
  path = path_excel.get()
37
38
  path_pdf = path_p.get()
39
+
40
+ # コンボボックス内の値の取得
41
+ #def select_combo(event):
42
+ #global sheetA
43
+ #sheet = combobox.get()
44
+ #print(sheet)
38
45
  excel = win32com.client.Dispatch("Excel.Application")
39
46
  excel.Visible=True
40
47
  files = []
@@ -58,6 +65,7 @@
58
65
  finally:
59
66
  excel.Quit()
60
67
 
68
+ #Excel to JPG
61
69
  def excel_JPG():
62
70
 
63
71
  path_pdf = path_p.get()
@@ -71,128 +79,148 @@
71
79
  print(files_pdf)
72
80
 
73
81
  for k in range(0,len(files_pdf)):
82
+ # PDFファイルのパス
74
83
  pdf_path = path_pdf + '/' + files_pdf[k]
75
84
 
85
+ #この1文で変換されたjpegファイルが、imageホルダー内に作られます。
76
86
  convert_from_path(pdf_path, output_folder=path_jpg,fmt='jpeg',output_file=Path(pdf_path).stem)
77
87
 
88
+ # Excelフォルダ指定先参照ボタンの関数
78
89
  def excel_dirdialog_clicked():
79
90
  iDir = os.path.abspath(os.path.abspath("__file__"))
80
91
  iDirPath = filedialog.askdirectory(initialdir = iDir)
81
92
  path_excel.set(iDirPath)
82
93
 
94
+ # pdf出力先指定の関数
83
95
  def pdf_outdirdialog_clicked():
84
96
  iDir_out = os.path.abspath(os.path.abspath("__file__"))
85
97
  iDirPath_out = filedialog.askdirectory(initialdir = iDir_out)
86
98
  path_p.set(iDirPath_out)
87
99
 
100
+ # jpg出力先指定の関数
88
101
  def jpg_outdirdialog_clicked():
89
102
  iDir_out = os.path.abspath(os.path.abspath("__file__"))
90
103
  iDirPath_out = filedialog.askdirectory(initialdir = iDir_out)
91
104
  path_j.set(iDirPath_out)
92
105
 
106
+ #SHEET名の取得関数
93
107
  def main():
94
108
  global statusbar
95
109
 
110
+ # ファイルの参照
96
111
  def brouse_func():
97
112
 
98
113
  fTyp = [("", ".xlsx")]
99
114
  file_name = filedialog.askopenfilename(filetypes=fTyp)
100
115
  statusbar["text"] = file_name
101
116
 
117
+ # コンボボックスのリスト取得
118
+ # ファイル読み込み
102
119
  wb = xl.load_workbook(filename=file_name)
103
120
  sheets = wb.sheetnames
104
121
 
105
- combobox.config(values=sheets)
122
+ combobox.config(values=sheets) #これでコンボボックスのリストを更新できる
123
+
106
-
124
+ # コンボボックス内の値の取得
107
125
  def select_combo(event):
108
126
  global sheet
109
127
  sheet = combobox.get()
110
128
 
129
+ #GUIの設定
111
130
  root = tk.Tk()
112
131
  root.title("Excel変換ツール")
113
132
  root.geometry("600x300")
114
133
 
134
+ # Frame1(Excel)の作成
115
135
  frame1 = ttk.Frame(root, padding=10)
116
136
  frame1.grid(row=0, column=1, sticky=E)
117
137
 
138
+ # 「Excelフォルダ参照」ラベルの作成
118
139
  IDirLabel = ttk.Label(frame1, text="Excel読込先指定>>", padding=(5, 2))
119
140
  IDirLabel.pack(side=LEFT)
120
141
 
142
+ # 「Excelフォルダ参照」エントリーの作成
121
143
  path_excel = StringVar()
122
144
  IDirEntry = ttk.Entry(frame1, textvariable=path_excel, width=60)
123
145
  IDirEntry.pack(side=LEFT)
124
146
 
147
+ # 「Excelフォルダ参照」ボタンの作成
125
148
  IDirButton = ttk.Button(frame1, text="参照", command=excel_dirdialog_clicked)
126
149
  IDirButton.pack(side=LEFT)
127
150
 
151
+ # Frame2(PDF出力先)の作成
128
152
  frame2 = ttk.Frame(root, padding=10)
129
153
  frame2.grid(row=2, column=1, sticky=E)
130
154
 
155
+ # 「PDF出力先」ラベルの作成
131
156
  IDirLabel_out = ttk.Label(frame2, text="PDF出力先指定>>", padding=(5, 2))
132
157
  IDirLabel_out.pack(side=LEFT)
133
158
 
159
+ # 「PDF出力先」エントリーの作成
134
160
  path_p = StringVar()
135
161
  IDirEntry_out = ttk.Entry(frame2, textvariable=path_p, width=60)
136
162
  IDirEntry_out.pack(side=LEFT)
137
163
 
164
+ # 「PDF出力先」ボタンの作成
138
165
  IDirButton_out = ttk.Button(frame2, text="参照", command=pdf_outdirdialog_clicked)
139
166
  IDirButton_out.pack(side=LEFT)
140
167
 
141
-
168
+ # Frame3(PDF出力先)の作成
142
169
  frame3 = ttk.Frame(root, padding=10)
143
170
  frame3.grid(row=4, column=1, sticky=E)
144
171
 
145
-
172
+ # 「jpg出力先」ラベルの作成
146
173
  IDirLabel_out = ttk.Label(frame3, text="JPG出力先指定>>", padding=(5, 2))
147
174
  IDirLabel_out.pack(side=LEFT)
148
175
 
149
-
176
+ # 「JPG出力先」エントリーの作成
150
177
  path_j = StringVar()
151
178
  IDirEntry_out = ttk.Entry(frame3, textvariable=path_j, width=60)
152
179
  IDirEntry_out.pack(side=LEFT)
153
180
 
154
-
181
+ # 「JPG出力先」ボタンの作成
155
182
  IDirButton_out_jpg = ttk.Button(frame3, text="参照", command=jpg_outdirdialog_clicked)
156
183
  IDirButton_out_jpg.pack(side=LEFT)
157
184
 
158
185
 
159
-
186
+ # Sheet Frame作成
160
187
  frame = ttk.Labelframe(root, padding=10)
161
188
  frame.grid(row=1, column=1, sticky=E)
162
189
 
163
-
190
+ # ステータスバー
164
191
  statusbar = tk.Label(frame,
165
192
  text="参照するファイルを選択してください。 ",
166
193
  bd=1, relief=tk.SUNKEN, anchor=tk.W)
167
194
  statusbar.pack(side=LEFT)
168
195
 
169
-
196
+ # 参照ボタン
170
197
  brouse_button = tk.Button(frame, text="参照", command=brouse_func)
171
198
  brouse_button.pack(padx=10, side=tk.LEFT)
172
199
 
173
-
200
+ ##コンボボックス
174
201
  v = tk.StringVar()
175
- sheets = []
202
+ sheets = [] #一旦、空リスト用意
176
203
  combobox = ttk.Combobox(frame, textvariable=v, values=sheets,
177
- height=3,
204
+ height=3, #コンボボックスクリックした時の表示数
178
- state="readonly")
205
+ state="readonly") #手入力禁止
179
- combobox.bind('<<ComboboxSelected>>', select_combo)
206
+ combobox.bind('<<ComboboxSelected>>', select_combo) #コンボボックスで選択した時、select_comboが発動
180
207
  combobox.pack(padx=10, pady=5, side=tk.BOTTOM)
181
208
 
182
-
209
+ # Frame(PDF変換 実行ボタン)の作成
183
210
  frame4 = ttk.Frame(root, padding=8)
184
211
  frame4.grid(row=5,column=1,sticky=W)
185
212
 
186
-
213
+ # PDF変換 実行ボタンの設置
187
214
  button1 = ttk.Button(frame4, text="Excel to PDF変換", command=excel_PDF)
188
215
  button1.pack(fill = "x", padx=30, side = "left")
189
216
 
190
-
217
+ # Frame(JPG変換 実行ボタン)の作成
191
218
  frame5 = ttk.Frame(root, padding=8)
192
219
  frame5.grid(row=6,column=1,sticky=W)
193
220
 
194
-
221
+ # JPG変換 実行ボタンの設置
195
222
  button2 = ttk.Button(frame5, text="PDF to JPG変換", command=excel_JPG)
196
223
  button2.pack(fill = "x", padx=30, side = "left")
197
224
 
198
225
  tk.mainloop()
226
+ ```