Home  >  Article  >  Backend Development  >  Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

WBOY
WBOYforward
2023-05-08 23:22:081897browse

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

Hello everyone, I am a rookie! Today I would like to share with you a very practical Python program.

Confusion Encountered

Many friends, whether in school or at work, will encounter a problem, which is to extract the text from PPT and save it to word Among them, this can facilitate your reading or printing out the text. But many times, friends can only extract the text in the PPT one by one by copying and pasting. This method of operation is undoubtedly very inefficient. Today, Brother Rookie will bring you a new method, using a program to extract text from PPT in batches and save it to a word document. Let’s take a look. .

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

#1. The appropriate scenario

For example, I have such a PPT content, which contains a lot of text and pictures. I am more interested in the text, especially in papers or ppts of some important academic reports, where a lot of text needs to be extracted and analyzed. Let me give you an example of a simple PPT page:

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

As you can see, the PPT above contains some text and picture content information, but I just want to extract text. In fact, this can be done easily with Python. Take a look at the final effect:

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

The effect is not bad, it is actually very simple. Yes, let’s see how to do it.

2. Program design

We mainly use the python-pptx library and the python-docx library. Used for processing PPT files and word files respectively. You can install it directly with pip3. The whole program is very short and concise. The core code only requires six lines. The program is as shown below:

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!


##The code is actually very short. In order to let everyone better understand this program, you can explain it one by one with the following picture.

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

In the program, we use a total of 3 levels of loops to process:

1). The first-level for loop is used to cycle through the slides of each page;

2) .In the second loop, determines each shape in the slide, and then determines whether the page contains a text box. If there is a text box, obtain the text box and name it text_frame.

3). The third for loop traverses all the paragraph contents in the text box, extracts the text and saves it to word among.

After traversing the entire PPT file, save all extracted text information to a local word document. The effect is as shown below:

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

The PPT file in the above picture contains four slides with text. After running the program, the text extraction results are shown in the figure below.

Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!

The above is the automation case sharing that Rookie brought to you today. With just a few lines of code, everyone's work can be greatly improved. Efficiency, everyone should also use the program for quick extraction.

The above is the detailed content of Extracting PPT text and transferring it to Word becomes a breeze, requiring only 20 lines of Python code!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete