Using Python batch extraction of Word documents in the image

Tags

Recently, I had to extract images from a Word document and save them due to work requirements.

Although the number of images is small, you can extract them manually, but if there are a lot of images in the word file, it will be very time-consuming, so here is a good way to share.

1.Importing the required packages

import zipfileimport shutil

2.Set the document path and image path and use zipfile to read Word files

doc_path = './text.docx' # Change you word file pathimage_path = './images/' # Change you iamges pathdoc = zipfile.ZipFile(doc_path)

3.Find the matching image file and copy the image file to the directory you specify, done!

for info in doc.infolist():    if info.filename.endswith((".png", ".jpeg", ".gif")):        doc.extract(info.filename, image_path)

Full Code

import zipfileimport shutil
doc_path = './text.docx' # Change you word file pathimage_path = './images/' # Change you iamges pathdoc = zipfile.ZipFile(doc_path)
for info in doc.infolist():    if info.filename.endswith((".png", ".jpeg", ".gif")):        doc.extract(info.filename, image_path)