Extract the pictures in the Word document and use python to batch convert the format

In your daily work, have you ever encountered such a scenario, the leader sent a Word document, asking you to store the pictures in the document in a folder, and also change the pictures to .jpg or .png, you What will it do? You are not collapse the inner side, while the beginning of a picture of Save As . Today, Tingyun teaches you two ways to save time and effort. No matter how many or even hundreds of pictures in the document, you can quickly save them.

1. Analysis

The application of pictures in documents is a very common phenomenon. Inserting suitable pictures in Word documents will undoubtedly make our documents more beautiful.

Think about it first, how do we usually insert pictures in Word?

  • Store the required picture materials in the local computer in advance, and then insert them into Word

  • Use copy, screenshot, etc. to paste the picture into Word

In fact, the second method has a drawback in that Word, if we need to save them to your local computer for future use pictures only exist, the most common method is to click the right mouse button and choose Save as Picture , and then select the path save.

This method is suitable when only a few pictures need to be processed. Once the number of pictures increases, the processing will become tedious and error-prone.

So, how can we save these pictures in batches?

Second, extract the pictures in the Word document

The solution is to change the file format and directly change the suffix of the Word document to a compressed format of .rar (.zip is also possible). Open the compressed file, click [word]-[media], the pictures used in the document will appear here, just select and unzip it.

The Word documents used for testing are as follows:

The operation method is as follows:

Click View, select detailed information, and check the file extension.



Directly change the suffix name of the Word document to the compressed format of .rar (.zip is also possible). Open the compressed file, click [word]-[media], the pictures used in the document will appear here, just select and unzip it.

Three, use python batch conversion format

# -*- coding: UTF-8 -*-
"""
@File    :test_01.py
@Author  :叶庭云
@CSDN    :https://yetingyun.blog.csdn.net/
"""
# 导入os模块
import os

# 不存在 jpg图片 这个文件夹  创建
if not os.path.exists('jpg图片'):
    os.mkdir('jpg图片')


path = r'.\jpg图片'
# 列出 media 文件夹下所有图片
files = os.listdir(r'.\media')

for item in files:
    # 拼接出media 文件夹下所有图片路径
    file_1 = '.\media' + '/' + item
    # 读取图片数据
    with open(file_1, 'rb') as f:
        con = f.read()
    # 重新写入  以 .jpg 格式 并保存到jog图片文件夹
    file_name = path + '/' + item.split('.')[0] + '.jpg'
    with open(file_name, 'wb') as f:
        f.write(con)

The operation effect is as follows:

The program runs, swish, the image format is converted to .jpg and saved in a new folder.

Author: Ye Tingyun
CSDN: https: //blog.csdn.net/fyfugoyfa
This article is only for the exchange of learning, without the author's permission prohibited reproduced, but not to other purposes, rights reserved.
If the article is helpful to you, you are welcome to give a like or star. Your support is the greatest encouragement to the author. You can correct the deficiencies in the comment area and exchange and learn.

Guess you like

Origin blog.csdn.net/fyfugoyfa/article/details/109381002