I want to extract images from a folder which consists of 600 word documents. Some of the…
- User
I want to extract images from a folder which consists of 600 word documents. Some of the documents are of doc format and the other are of docx. I want to extract images from all those documents. Can you give me some Python code?
- Assistant
Yes, here is a Python code that extracts images from all the doc and docx files in a folder:
import os import zipfile from PIL import Image # define the folder path folder_path = '/path/to/folder' # loop through all the files in the folder for filename in os.listdir(folder_path): # check if the file is a doc or docx file if filename.endswith('.doc') or filename.endswith('.docx'): # extract the images from the file with zipfile.ZipFile(os.path.join(folder_path, filename)) as docx: for name in docx.namelist(): if name.endswith('.jpeg') or name.endswith('.jpg') or name.endswith('.png'): with docx.open(name) as image_file: img = Image.open(image_file) img.save(os.path.join(folder_path, f"{filename.split('.')[0]}_{name.split('/')[-1]}"))This code uses the
osmodule to loop through all the files in the folder and thezipfilemodule to extract images from doc and docx files. It uses thePIL(Python Imaging Library) module to save the extracted images. - User
I am getting an error ‘file is not a zip file’ if I run the complete code that you produced above. Can you solve this error?
- Assistant
Yes, the error “file is not a zip file” occurs when the code tries to extract images from a non-zipped file or a file that is not a Word document. To avoid this error, we can add a try-except statement that checks whether the file is a zip file before extracting images. Here is the modified code:
import os import zipfile from PIL import Image # define the folder path folder_path = '/path/to/folder' # loop through all the files in the folder for filename in os.listdir(folder_path): # check if the file is a doc or docx file if filename.endswith('.doc') or filename.endswith('.docx'): # extract the images from the file try: with zipfile.ZipFile(os.path.join(folder_path, filename)) as docx: for name in docx.namelist(): if name.endswith('.jpeg') or name.endswith('.jpg') or name.endswith('.png'): with docx.open(name) as image_file: img = Image.open(image_file) img.save(os.path.join(folder_path, f"{filename.split('.')[0]}_{name.split('/')[-1]}")) except zipfile.BadZipFile: print(f"Skipping {filename}: Not a valid zip file.") except: print(f"Error while extracting images from {filename}") - User
The following Python code works for only one document. Can you make changes in the code such that it works on an entire folder of word documents in both doc and docx formats?
import win32com from win32com.client import Dispatch import docx import zipfile import os import shutil
def doc2docx(path): word = win32com.client.Dispatch('word.application') word.DisplayAlerts = 0 word.visible = 0 doc = word.Documents.Open(path) doc.SaveAs(path+"x", 12) doc.Close() word.Quit()
def extracttext(docxpath): doc = docx.Document(docxpath) fp = open("f:/tempbuffer.txt", "a", encoding="utf-8") for p in doc.paragraphs: fp.write(p.text+u"\n") fp.close()
def extractimgs(docxpath, dstpath): doc = zipfile.ZipFile(docxpath) for info in doc.infolist(): if info.filename.endswith((".png", ".jpeg", ".gif")): doc.extract(info.filename, dstpath) shutil.copy(dstpath+"\"+info.filename, dstpath+"\"+ docxpath.split("\")[-1] + info.filename.split("/")[-1]) doc.close()
docpath = "my.doc" docxpath = docpath + "x" doc2docx(docpath) extracttext(docxpath) extractimgs(docxpath, "f:\imgs")
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.