📜  Python .docx 模块中的段落格式

📅  最后修改于: 2022-05-13 01:55:08.578000             🧑  作者: Mango

Python .docx 模块中的段落格式

先决条件:使用 .docx 模块

Word 文档包含包装在三个对象级别内的格式化文本。最低级-运行对象,中间级-段落对象,和最高级-文档对象。因此,我们无法使用普通文本编辑器处理这些文档。但是,我们可以使用 python-docx 模块在Python中操作这些 word 文档。安装这个模块的pip命令是:

pip install python-docx

Python docx 模块允许用户通过操作现有文档或创建新的空文档并对其进行操作来操作文档。它是一个强大的工具,因为它可以帮助您在非常大的范围内操作文档。

行间距

为了设置段落中文本之间的行距,我们使用了paragraph_formatline_spacing。它用于设置段落中每行之间的间距。

例 1:用绝对距离值设置行距。

Python3
# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with spacing
doc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
# Adding line space of 0.5 inches in the paragraph
para.paragraph_format.line_spacing = Inches(0.5)
  
# Adding paragraph without spacing
doc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
  
# Now save the document to a location 
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with spacing
doc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
# Adding line space in the paragraph
para.paragraph_format.line_spacing = 1.75
  
# Adding paragraph without spacing
doc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
  
# Now save the document to a location 
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with spacing
doc.add_heading('Paragraph Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Adding space before and after of the paragraph
para.paragraph_format.space_before = Inches(0.25)
para.paragraph_format.space_after = Inches(0.25)
  
# Adding paragraph without spacing
doc.add_heading('Paragraph Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
  
# Now save the document to a location 
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
from docx.enum.text import WD_ALIGN_PARAGRAPH
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with alignment Center
doc.add_heading('Alignment: Center', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
  
# Adding paragraph with alignment Left
doc.add_heading('Alignment: Left', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
  
# Adding paragraph with alignment Right
doc.add_heading('Alignment: Right', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT
  
# Adding paragraph with alignment Justify
doc.add_heading('Alignment: Justify', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
  
# Adding paragraph with alignment Distribute
doc.add_heading('Alignment: Distribute', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.DISTRIBUTE
  
# Now save the document to a location 
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with left Indentation
doc.add_heading('Indentation: Left', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal \
for geeks. It contains well written, well thought and well-explained \
computer science and programming articles, quizzes etc.')
para.paragraph_format.left_indent = Inches(0.5)
  
# Adding paragraph with right Indentation
doc.add_heading('Indentation: Right', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal\
for geeks. It contains well written, well thought and well-explained\
computer science and programming articles, quizzes etc.')
para.paragraph_format.right_indent = Inches(0.5)
  
# Now save the document to a location 
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with negative left Indentation
doc.add_heading('Indentation: Left', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal \
for geeks. It contains well written, well thought and well-explained\
computer science and programming articles, quizzes etc.')
para.paragraph_format.left_indent = Inches(-0.5)
  
# Adding paragraph with negative right Indentation
doc.add_heading('Indentation: Right', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal\
for geeks. It contains well written, well thought and well-explained\
computer science and programming articles, quizzes etc.')
para.paragraph_format.right_indent = Inches(-0.5)
  
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with only first line Indented
doc.add_heading('Indentation: First Line', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science\
portal for geeks. It contains well written, well thought and \
well-explained computer science and programming articles, quizzes etc.')
  
# Causing First Line Indentation
para.paragraph_format. first_line_indent = Inches(0.5)
  
# Now save the document to a location 
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with only first line Indented
doc.add_heading('Indentation: First Line', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
  
# Causing First Line Indentation
para.paragraph_format. first_line_indent = Inches(-0.5)
  
# Now save the document to a location 
doc.save('gfg.docx')


输出:

例2:用相对值设置行距。

蟒蛇3

# Import docx NOT python-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with spacing
doc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
# Adding line space in the paragraph
para.paragraph_format.line_spacing = 1.75
  
# Adding paragraph without spacing
doc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
  
# Now save the document to a location 
doc.save('gfg.docx')

输出:

段落间距

为了将段落间距应用于 Word 文档中的段落,我们使用.paragraph_format以及.space_before.space_after 。它分别指定了段落前后要留下的空间。它只能以正值作为输入,如果我们给出任何负值,它将给出范围误差

Sr. No.

Spacing

Description

1.

.space_before

It adds space before the paragraph in the word document.

2.

.space_after

It adds space after the paragraph in the word document.

示例 3:在 Word 文档中添加带间距和不带间距的段落。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with spacing
doc.add_heading('Paragraph Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Adding space before and after of the paragraph
para.paragraph_format.space_before = Inches(0.25)
para.paragraph_format.space_after = Inches(0.25)
  
# Adding paragraph without spacing
doc.add_heading('Paragraph Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
  
# Now save the document to a location 
doc.save('gfg.docx')

输出:

水平对齐

要在文本中设置水平对齐方式,我们将使用.paragraph_format.alignment方法。它与WD_PARAGRAPH_ALIGNMENT一起使用来设置段落的对齐方式。在使用它之前,您必须从docx.enum.text导入WD_PARAGRAPH_ALIGNMENT

from docx.enum.text import WD_ALIGN_PARAGRAPH

各种对齐描述如下:

Sr. No.

Alignment Name

Description

1.

CENTER

It sets the alignment to Centre.

2.

LEFT

It sets the alignment to left.

3.

RIGHT

It sets the alignment to right.

4.

JUSTIFY

It sets the alignment to justify.

5.

DISTRIBUTE

It sets the characters in such a way that they fill the entire width of the paragraph.

示例 1:添加具有不同水平对齐方式的段落。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.enum.text import WD_ALIGN_PARAGRAPH
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with alignment Center
doc.add_heading('Alignment: Center', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
  
# Adding paragraph with alignment Left
doc.add_heading('Alignment: Left', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
  
# Adding paragraph with alignment Right
doc.add_heading('Alignment: Right', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT
  
# Adding paragraph with alignment Justify
doc.add_heading('Alignment: Justify', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
  
# Adding paragraph with alignment Distribute
doc.add_heading('Alignment: Distribute', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.DISTRIBUTE
  
# Now save the document to a location 
doc.save('gfg.docx')

输出:

压痕

要在文本中设置缩进,我们将使用.paragraph_format方法。为了应用缩进,我们使用left_indentright_indent.paragraph_format并设置缩进的值。您必须使用长度值指定缩进,即英寸、磅或厘米。您还可以给出一个负值作为缩进,这将导致段落与边距重叠指定的值。

Sr. No.

Indentation

Description

1.

left_indent

It sets the left indentation of the paragraph in the word file.

2.

right_indent

It sets the right indentation of the paragraph in the word file.

示例二:设置段落左右缩进。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with left Indentation
doc.add_heading('Indentation: Left', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal \
for geeks. It contains well written, well thought and well-explained \
computer science and programming articles, quizzes etc.')
para.paragraph_format.left_indent = Inches(0.5)
  
# Adding paragraph with right Indentation
doc.add_heading('Indentation: Right', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal\
for geeks. It contains well written, well thought and well-explained\
computer science and programming articles, quizzes etc.')
para.paragraph_format.right_indent = Inches(0.5)
  
# Now save the document to a location 
doc.save('gfg.docx')

输出:

示例 3:为段落的左右缩进设置负值。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with negative left Indentation
doc.add_heading('Indentation: Left', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal \
for geeks. It contains well written, well thought and well-explained\
computer science and programming articles, quizzes etc.')
para.paragraph_format.left_indent = Inches(-0.5)
  
# Adding paragraph with negative right Indentation
doc.add_heading('Indentation: Right', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal\
for geeks. It contains well written, well thought and well-explained\
computer science and programming articles, quizzes etc.')
para.paragraph_format.right_indent = Inches(-0.5)
  
# Now save the document to a location
doc.save('gfg.docx')

输出:

您还可以使用.paragraph_format.first_line_indent属性仅为段落的第一行设置缩进。它指定第一行和其他行之间的缩进长度。

示例 4:给出正缩进值作为第一行缩进的输入。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with only first line Indented
doc.add_heading('Indentation: First Line', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science\
portal for geeks. It contains well written, well thought and \
well-explained computer science and programming articles, quizzes etc.')
  
# Causing First Line Indentation
para.paragraph_format. first_line_indent = Inches(0.5)
  
# Now save the document to a location 
doc.save('gfg.docx')

输出:

示例 5:给出负缩进值作为第一行缩进的输入。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.shared import Inches
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph with only first line Indented
doc.add_heading('Indentation: First Line', 3)
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
  
# Causing First Line Indentation
para.paragraph_format. first_line_indent = Inches(-0.5)
  
# Now save the document to a location 
doc.save('gfg.docx')

输出: