import
tkinter as tk
from
tkinter
import
filedialog
from
docx
import
Document
from
docx.shared
import
Inches
import
os
def
get_folder_path():
folder_selected
=
filedialog.askdirectory()
folder_path.
set
(folder_selected)
def
get_output_file_path():
output_file_selected
=
filedialog.asksaveasfilename(
defaultextension
=
".docx"
,
filetypes
=
[(
"Word文档"
,
"*.docx"
), (
"所有文件"
,
"*.*"
)],
title
=
"选择输出文件的位置和名称"
)
output_filename.
set
(output_file_selected)
def
create_document():
img_folder
=
folder_path.get()
output_file
=
output_filename.get()
if
not
img_folder:
status_label.config(text
=
'请先选择图片文件夹。'
)
return
if
not
output_file:
status_label.config(text
=
'请先选择输出文件的位置和名称。'
)
return
image_paths
=
[os.path.join(img_folder, f)
for
f
in
os.listdir(img_folder)
if
f.lower().endswith((
'.jpg'
,
'.jpeg'
,
'.png'
))]
if
not
image_paths:
status_label.config(text
=
'在指定的文件夹中没有找到支持的图片格式。'
)
return
status_label.config(text
=
'正在创建文档...'
)
window.update()
doc
=
Document()
width
=
Inches(
2.8
)
height
=
Inches(
1.7
)
table
=
doc.add_table(rows
=
0
, cols
=
2
)
for
i, img_path
in
enumerate
(image_paths):
if
i
%
2
=
=
0
:
row_cells
=
table.add_row().cells
cell
=
row_cells[i
%
2
]
para
=
cell.add_paragraph()
run
=
para.add_run()
try
:
pic
=
run.add_picture(img_path, width
=
width, height
=
height)
para.alignment
=
1
file_name_para
=
cell.add_paragraph(os.path.basename(img_path))
file_name_para.alignment
=
1
status_label.config(text
=
f
'{os.path.basename(img_path)} 已插入到 word 文档中...'
)
window.update()
except
Exception as e:
status_label.config(text
=
f
'无法插入 {os.path.basename(img_path)},原因:{e}'
)
window.update()
if
image_paths
and
(i
+
1
)
%
2
!
=
0
:
status_label.config(text
=
'最后一行只有一个图片,如有需要请手动调整格式。'
)
try
:
doc.save(output_file)
status_label.config(text
=
f
'所有图片已全部插入,文档保存为 "{os.path.basename(output_file)}"。'
)
window.update()
open_document(output_file)
except
Exception as e:
status_label.config(text
=
f
'无法保存文档,原因:{e}'
)
def
open_document(file_path):
try
:
if
os.path.isfile(file_path):
if
os.name
=
=
'nt'
:
os.startfile(file_path)
elif
os.name
=
=
'posix'
:
os.system(f
'open "{file_path}"'
)
else
:
status_label.config(text
=
'不支持的操作系统,无法自动打开文档。'
)
except
Exception as e:
status_label.config(text
=
f
'无法打开文档,原因:{e}'
)
window
=
tk.Tk()
window.title(
'图片插入Word文档工具'
)
folder_path
=
tk.StringVar()
output_filename
=
tk.StringVar()
tk.Label(window, text
=
'图片文件夹路径:'
).grid(row
=
0
, column
=
0
)
entry_folder_path
=
tk.Entry(window, textvariable
=
folder_path, width
=
40
).grid(row
=
0
, column
=
1
)
browse_button_folder
=
tk.Button(window, text
=
'浏览'
, command
=
get_folder_path).grid(row
=
0
, column
=
2
)
tk.Label(window, text
=
'输出文件路径:'
).grid(row
=
1
, column
=
0
)
entry_output_filename
=
tk.Entry(window, textvariable
=
output_filename, width
=
40
).grid(row
=
1
, column
=
1
)
browse_button_output
=
tk.Button(window, text
=
'浏览'
, command
=
get_output_file_path).grid(row
=
1
, column
=
2
)
start_button
=
tk.Button(window, text
=
'开始'
, command
=
create_document).grid(row
=
2
, column
=
1
)
status_label
=
tk.Label(window, text
=
'
', fg='
blue')
status_label.grid(row
=
3
, column
=
1
)
window.mainloop()