Python實現(xiàn)批量合并圖片到word文檔
這段代碼是一個用Python編寫的功能,它將指定文件夾中的所有圖片插入到Word文檔中并保存。以下是代碼的主要步驟和功能:
導入必要的庫
Python中的docx
庫用于操作Word文檔,glob
庫用于匹配文件路徑。
from docx import Document from docx.shared import Inches import glob
定義函數(shù) insert_images_to_word(image_folder, output_path)
該函數(shù)用于將指定文件夾中的所有圖片插入到Word文檔中,并保存到指定路徑。
def insert_images_to_word(image_folder, output_path): try: # 創(chuàng)建一個空白的Word文檔對象 doc = Document() # 添加段落 doc.add_paragraph('這是一個包含多張圖片的Word文檔') # 獲取文件夾中的所有圖片文件路徑 image_files = glob.glob(image_folder + '/*.png') + glob.glob(image_folder + '/*.jpg') # 遍歷圖片文件路徑列表,逐個添加圖片到Word文檔中 for image_file in image_files: doc.add_picture(image_file, width=Inches(6), height=Inches(3)) # 保存Word文檔 doc.save(output_path) return True except Exception as e: print(e) return False
創(chuàng)建一個空白的Word文檔對象
doc = Document()
添加一個段落到Word文檔中
doc.add_paragraph('這是一個包含多張圖片的Word文檔')
獲取文件夾中的所有圖片文件路徑
使用glob.glob()
函數(shù)獲取文件夾中所有的.png
和.jpg
圖片文件路徑。
image_files = glob.glob(image_folder + '/*.png') + glob.glob(image_folder + '/*.jpg')
遍歷圖片文件路徑列表,逐個將圖片添加到Word文檔中
利用add_picture()
方法將圖片逐個添加到Word文檔中,可以設置圖片的寬度和高度。
for image_file in image_files: doc.add_picture(image_file, width=Inches(6), height=Inches(3))
保存Word文檔到指定路徑
使用save()
方法保存Word文檔到指定的輸出路徑。
doc.save(output_path)
返回生成結(jié)果狀態(tài)
如果成功生成Word文檔,則返回True
,否則返回False
。
return True
調(diào)用函數(shù)并輸出結(jié)果
最后,根據(jù)需要指定圖片所在文件夾路徑和輸出文件路徑,并調(diào)用函數(shù)進行操作。根據(jù)返回值判斷操作是否成功。
# 圖片所在文件夾路徑 image_folder = r'C:\Users\Admin\Desktop\數(shù)據(jù)核對' # Word文檔保存路徑 output_path = r'C:\Users\Admin\Desktop\output.docx' # 調(diào)用函數(shù) if insert_images_to_word(image_folder, output_path): print('Word文檔已保存到{}'.format(output_path)) else: print('生成Word文檔失敗。')
這樣,代碼就完成了將指定文件夾中的所有圖片插入到Word文檔并保存的功能。
完整代碼一
from docx import Document from docx.shared import Inches import glob def insert_images_to_word(image_folder, output_path): """ 將指定文件夾中的所有圖片插入到 Word 文檔中,并保存到指定路徑。 image_folder:圖片所在文件夾的路徑。 output_path:Word 文檔保存路徑。 """ try: # 創(chuàng)建一個空白的Word文檔對象 doc = Document() # 添加段落 doc.add_paragraph('這是一個包含多張圖片的Word文檔') # 獲取文件夾中的所有圖片文件路徑 image_files = glob.glob(image_folder + '/*.png') + glob.glob(image_folder + '/*.jpg') # 遍歷圖片文件路徑列表,逐個添加圖片到Word文檔中 for image_file in image_files: doc.add_picture(image_file, width=Inches(6), height=Inches(3)) # 保存Word文檔 doc.save(output_path) return True except Exception as e: print(e) return False # 圖片所在文件夾路徑 image_folder = r'C:\Users\Admin\Desktop\數(shù)據(jù)核對' # Word文檔保存路徑 output_path = r'C:\Users\Admin\Desktop\output.docx' # 調(diào)用函數(shù) if insert_images_to_word(image_folder, output_path): print('Word文檔已保存到{}'.format(output_path)) else: print('生成Word文檔失敗。')
完整代碼二
from docx import Document from docx.shared import Inches import glob # 創(chuàng)建一個空白的Word文檔對象 doc = Document() # 添加段落 doc.add_paragraph('這是一個包含多張圖片的Word文檔') # 圖片所在文件夾路徑 image_folder = r'C:\Users\liuchunlin2\Desktop\新建文件夾' # 獲取文件夾中的所有圖片文件路徑 image_files = glob.glob(image_folder + '/*.png')+glob.glob(image_folder + '/*.jpg') # 根據(jù)實際情況修改文件擴展名 print(image_files) # 遍歷圖片文件路徑列表,逐個添加圖片到Word文檔中 for image_file in image_files: doc.add_picture(image_file, width=Inches(6), height=Inches(3)) # 保存Word文檔 doc.save('output.docx')
到此這篇關于Python實現(xiàn)批量合并圖片到word文檔的文章就介紹到這了,更多相關Python合并圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python 寫函數(shù)在一定條件下需要調(diào)用自身時的寫法說明
這篇文章主要介紹了python 寫函數(shù)在一定條件下需要調(diào)用自身時的寫法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06