Python docx operation word, insert the picture at the specified location

Posted by markjia on Wed, 10 Jun 2020 08:13:16 +0200

python-docx operates on word and inserts the picture in the specified position.

introduce

This article mainly describes the positioning method. When we want to insert some pictures in the document, add in Python docx_ Picture is always inserted at the end of the document, so we need to locate, including table positioning and paragraph positioning. Table positioning is more accurate, but sometimes our word is not a table at all, so we can only locate paragraphs. However, the picture inserted after the paragraph positioning is still at the end of the document. Using run object to solve.

Install Python docx

pip install python-docx # Blogger installed 0.8.10

Import package

import docx  # docx is used to connect word

Connect the word document to be operated on

doc = docx.Document(file_dir_path) 
//For example:
doc = docx.Document(r"D:\test.docx") # connect test.docx file

Add text

doc.add_paragraph('text....')

Add picture

doc.add_picture(r'D:\picture.png')

Add table

tab = doc.add_table(rows=4, cols=4)  # Add an empty table with 4 rows and 4 columns

cell = tab.cell(1, 3)  # Get the table object of the second row and three columns (the index starts from 0)
# To add text to a cell:
cell.text='Text to add'

run object

If you want the text in the same paragraph to be formatted differently, you need to use the Run object (which can be understood as a paragraph object that can be formatted separately).
run = paragraph = doc.add_paragraph('text content '). add_run('text content ')
run.bold  =True? Set font to bold
 chg_font(run, fontname = 'Microsoft YaHei', size=Pt(12)) 񖓿 set font and font size

location

Table positioning

First of all, you must have a table in your word to locate. The table location is accurate

Paragraph positioning (text positioning)

    for i, p in enumerate(doc.paragraphs): # Traverse all paragraphs
        print(str(i) + ":"+ str(p.text))
        if len(p.text) != 0:
            for i in range(len(p.runs)): # p.runs represents a list of all the text under the P paragraph
                print(p.runs[i].text)  # When printing, it is found that p.runs automatically decomposes the paragraphs
        if 'What you want to position' in p.text: 
            p.runs[-1].add_break()  # Add a break
            p.runs[-1].add_picture(photo_dit_path) # Add a picture after the last text in runs

Complete code

import sys
import docx
import os
'''
//Traverse the directory to insert pictures in wrod.
'''
def file_name_walk(file_dir, kwword):
    """
    //Return the absolute path of the required file (test report on the right side of the short side)
    :param file_dir: catalog
    :param kwword: Document No
    :return:
    """
    for root, dirs, files in os.walk(file_dir):
        # print("root", root)  # Current directory path
        # print("dirs", dirs)  # All subdirectories under the current path
        # print("files", files)  # All non directory sub files under the current path
        for f in files:
            if kwword in f:
                return root + "\\" + f
# Get the name of the picture and separate by commas
def photo_name_walk(photo_dir,file_dir, suffix='.png'):
    """
    //Get the number (or name) of the word from the picture name
    :param photo_dir: Picture path
    :param file_dir: To insert wrod Path to file
    :param suffix: file extension
    :return:
    """
    for root, dirs, files in os.walk(photo_dir):
        for f in files:
            if os.path.splitext(f)[1] == suffix:
                photo_name = os.path.splitext(f)[0]
                # Divide photo s by commas
                file_number = photo_name.split(',')[0]  # Using file_number find the corresponding word document
                position_number = photo_name.split(',')[1]  # Use position to find the place where the document is to be inserted
                # Absolute path of photo
                photo_dit_path = root + "\\" + f
                # Find the absolute path of word
                file_dir_path = file_name_walk(file_dir, file_number)
                # Insert picture in word
                word_main(file_dir_path, photo_dit_path, position_number)


def word_main(file_dir_path, photo_dit_path, position_number):
    '''
    //Insert picture in word
    :param file_dir_path: File absolute path
    :param photo_dit_path: Absolute path of picture
    :param position_number: Where to locate word Middle search position_number This string
    :return:
    '''
    doc = docx.Document(file_dir_path)
    for i, p in enumerate(doc.paragraphs):  # Traverse all paragraphs
        print(str(i) + ":"+ str(p.text))
        if len(p.text) != 0:

            for i in range(len(p.runs)):  # p.runs represents a list of all the text under the P paragraph
                print(str(i)+':::::')
                print(p.runs[i].text)  # When printing, it is found that p.runs automatically decomposes the paragraphs
        if position_number in p.text:
            p.runs[-1].add_break()  # Add a break
            p.runs[-1].add_picture(photo_dit_path)  # Add a picture after the last text in runs
            # os.remove(photo_dit_path)

        doc.save(file_dir_path)  # Save file
file_dir = sys.argv[1]
# print('file_dir', file_dir)
photo_dir = sys.argv[2]
# print('photo_dir', photo_dir)
photo_name_walk(photo_dir, file_dir)

[reference documents]
[1]: https://www.cnblogs.com/xied/p/12619571.html

Topics: Python pip