4. python-docx inserts paragraphs into word

Method 1

1. Import module

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

2. Insert paragraph

# Insert paragraph
p1 = document.add_paragraph('At this time, the mood is at this time, nothing happens to the little fairy.')
p2 = document.add_paragraph('Wake up from a dream in the afternoon, people are quiet at the small window, spring is in the sound of flower sellers.', style='List Bullet')
p3 = document.add_paragraph('I sing and dance to my heart's content, and my joy is unrestricted.', style='List Number')
# Content is centered
p1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

illustrate:

style sets paragraph style, there are two types:

①List Bullet: Bullets

②List Number: List number

3. Complete code

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import Pt, RGBColor

document = Document()
document.styles['Normal'].font.name = 'Times New Roman'
document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
"""Set the text font size"""
document.styles['Normal'].font.size = Pt(18)
"""Set text font color"""
document.styles['Normal'].font.color.rgb = RGBColor(255, 0, 0)
# insert paragraph
p1 = document.add_paragraph('At this time, the mood is at this time, nothing happens to the little fairy.')
p2 = document.add_paragraph('Wake up from a dream in the afternoon, people are quiet at the small window, spring is in the sound of flower sellers.', style='List Bullet')
p3 = document.add_paragraph('I sing and dance to my heart's content, and my joy is unrestricted.', style='List Number')
# Content is centered
p1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
document.save('insert paragraph 1.docx')

result:

The content in the paragraph defaults to the main text. All the content in the paragraph will be applied with the main text style as a whole. If you want to have special processing, use the second method.

Method 2

1. Import module

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx. shared import Pt, RGBColor

2. Insert paragraph

# insert paragraph 1 horizontal left
p1 = document.add_paragraph('')
run = p1.add_run('If you have nothing to worry about, it is a good time in the world.')
run.font.color.rgb = RGBColor(0, 255, 255)
run.font.size = Pt(14)
run.font.bold = True
p1.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT

# Insert paragraph 3 horizontally to the left, first line indented two characters
p3 = document.add_paragraph('')
run = p3.add_run('Laughing up to the sky and going out, we are from Penghao.')
run.font.color.rgb = RGBColor(255, 0, 200)
run.font.size = Pt(14)
run.font.bold = True
p3.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
#Indent the first line by 2 characters
p3.paragraph_format.first_line_indent = Pt(28)

# Insert paragraph 4, insert a large section of content, add special characters \\
 \r, set the line spacing, the space after the short preceding paragraph
p4 = document.add_paragraph('')
run = p4.add_run(
    'After seeing a hundred singles and eight generals, the water in the water pool hits the reeds. \\
Who came to Liangshan, and the top spot was placed in the hall. \\
The rod of the great monk who beats the tiger, and the food of Xiao Er in the tavern is hot. \\
My brothers love to drink. Drink happily and pull out a weeping willow, and often get embarrassed if you don't have the strength. \tThe sharpening stone is hard, and you feel panicked when the sword is drawn. ')
run.font.size = Pt(14)
run.font.color.rgb = RGBColor(45, 12, 90)
p4.paragraph_format.first_line_indent = Pt(28)
# Line spacing
p4.paragraph_format.line_spacing = 2
# Give an example of 50 pounds after the paragraph
p4.space_after = Pt(50)
# 50 pounds before section
p4.space_before = Pt(50)

# insert paragraph 2 centered horizontally
p2 = document.add_paragraph('')
run = p2.add_run('Laughing up to the sky and going out, we are from Penghao.')
run.font.color.rgb = RGBColor(0, 255, 0)
run.font.size = Pt(14)
run.font.bold = True
p2.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

3. Complete code

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx. shared import Pt, RGBColor

document = Document()
document.styles['Normal'].font.name = 'Times New Roman'
document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
"""Set the size of the text font"""
document.styles['Normal'].font.size = Pt(18)
"""Set text font color"""
document.styles['Normal'].font.color.rgb = RGBColor(255, 0, 0)

# insert paragraph 1 horizontal left
p1 = document.add_paragraph('')
run = p1.add_run('If you have nothing to worry about, it is a good time in the world.')
run.font.color.rgb = RGBColor(0, 255, 255)
run.font.size = Pt(14)
run.font.bold=True
p1.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT

# Insert paragraph 3 horizontally to the left, first line indented two characters
p3 = document.add_paragraph('')
run = p3.add_run('Laughing up to the sky and going out, we are from Penghao.')
run.font.color.rgb = RGBColor(255, 0, 200)
run.font.size = Pt(14)
run.font.bold=True
p3.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
# Indent the first line by 2 characters
p3.paragraph_format.first_line_indent = Pt(28)

# Insert paragraph 4, insert a large section of content, add special characters \\
 \r, set the line spacing, the space after the short preceding paragraph
p4 = document.add_paragraph('')
run = p4.add_run(
    'After seeing a hundred singles and eight generals, the water in the water pool hits the reeds. \\
Who came to Liangshan, and the top spot was placed in the hall. \\
The rod of the great monk who beats the tiger, and the food of Xiao Er in the tavern is hot. \\
My brothers all like to drink. Drink happily and pull out a weeping willow, and often get embarrassed if you don't have the strength. \tThe sharpening stone is hard, and you feel panicked when the sword is drawn. ')
run.font.size = Pt(14)
run.font.color.rgb = RGBColor(45, 12, 90)
p4.paragraph_format.first_line_indent = Pt(28)
# Line spacing
p4.paragraph_format.line_spacing = 2
# example 50 lbs after the paragraph
p4. space_after = Pt(50)
# 50 lbs before paragraph
p4. space_before = Pt(50)

# Insert paragraph 2 horizontally centered
p2 = document.add_paragraph('')
run = p2.add_run('Laughing up to the sky and going out, we are from Penghao.')
run.font.color.rgb = RGBColor(0, 255, 0)
run.font.size = Pt(14)
run.font.bold = True
p2.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

document.save('Insert paragraph 2.docx')

result:

4. Summarize common technical points in paragraphs
4.1 Indentation and distance before/after paragraph

First line indent
ParagraphObject.paragraph_format.first_line_indent = Pt(28)
Distance before segment
   Paragraph object.space_before = Pt(50)
Distance after segment
Paragraph object.space_after = Pt(50)

4.2 Horizontal direction

Left (default)
ParagraphObject.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
Centered
ParagraphObject.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
right
ParagraphObject.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT

4.3 Set the size, color, etc.

Set font

Set size
p4 = document.add_paragraph('')
run = p4.add_run(
    'After seeing a hundred singles and eight generals, the water in the water pool hits the reeds. \\
Who came to Liangshan, and the top spot was placed in the hall. \\
The rod of the great monk who beats the tiger, and the food of Xiao Er in the tavern is hot. \\
My brothers love to drink. Drink happily and pull out a weeping willow, and often get embarrassed if you don't have the strength. \tThe sharpening stone is hard, and you feel panicked when the sword is drawn. ')
 # Set font size
run.font.size = Pt(14)
Set color
p4 = document.add_paragraph('')
run = p4.add_run(
    'After seeing a hundred singles and eight generals, the water in the water pool hits the reeds. \\
Who came to Liangshan, and the top spot was placed in the hall. \\
The rod of the great monk who beats the tiger, and the food of Xiao Er in the tavern is hot. \\
My brothers love to drink. Drink happily and pull out a weeping willow, and often get embarrassed if you don't have the strength. \tThe sharpening stone is hard, and you feel panicked when the sword is drawn. ')
# set the color
run.font.color.rgb = RGBColor(45, 12, 90)