需求:将一个文件夹中的所有图片复制进word文档中
进度:已经实现复制函数,单线程已经跑通实现
实现效果:文件夹中的几十张图片通过多线程实现同时复制进word
要求:在源代码中修改多线程部分
```python
from docx import Document
from docx.shared import Inches
import os
from PIL import Image
import threading
def pic_to_word():
#图片所在文件夹路径
folder="D:\\shu_pic_srrc"
for root, dirs, pics in os.walk(folder):
doc=Document()
for i in range(0,len(pics)):
filepath = 'D:\\shu_pic_srrc\\'+pics[i]
try:
doc.add_picture(filepath,width=Inches(6),height=Inches(4))
except Exception:
pic_tmp=Image.open(filepath)
pic_tmp.save(pic_tmp)
doc.add_picture(filepath, width=Inches(6),height=Inches(4))
doc.save('D:\\shu_pic_srrc\\shu_pic_srrc.docx')
print("pic", i + 1, "successfully added.")
if __name__ == '__main__':
threads = []
folder = "D:\\shu_pic_srrc"
for root, dirs, pics in os.walk(folder):
for i in range(0, len(pics)):
threads.append(threading.Thread(target=pic_to_word))
for thread in threads:
thread.start()
```