📜  在Python .docx 模块中使用段落

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

在Python .docx 模块中使用段落

先决条件: docx

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

pip install python-docx

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

添加段落

要在 word 文档中添加一个段落,我们使用内置方法add_paragraph()在 word 文档中添加一个段落。通过使用这种方法,我们甚至可以添加包含像“\n”、“\t”和“\r”这样的字符的段落。除此之外,我们还可以为其添加各种样式。

示例 1:在 Word 文档中添加段落的Python程序。

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
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 multilined paragraph
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 Normal Texted paragraph
doc.add_heading('Normal Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Normal')
  
# Adding Body Text Styled paragraph
doc.add_heading('Body Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Body Text')
  
# Adding Caption Styled paragraph
doc.add_heading('Caption Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Caption')
  
# Adding Title Styled paragraph
doc.add_heading('Title Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Title')
  
# Adding Heading Styled paragraph
doc.add_heading('Heading 1 Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Heading 1')
  
# Adding Macro Text Styled paragraph
doc.add_heading('Macro Text Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'macro')
  
# Adding Quoted Style paragraph
doc.add_heading('Quoted Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Quote')
  
# Adding TOC Heading Styled paragraph
doc.add_heading('TOC Heading Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'TOC Heading')
  
# Adding Subtitle Styled paragraph
doc.add_heading('Subtitle Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Subtitle')
  
# Adding No Spacing Styled paragraph
doc.add_heading('No Spacing Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'No Spacing')
  
# Now save the document to a location 
doc.save('gfg.docx')


输出:

可以通过在方法中输入多行字符串来插入多行段落,这可以通过使用三个单引号“'Geeksforgeeks”'轻松完成。

示例 2:在 Word 文档中添加多行段落的Python程序。

蟒蛇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 multilined paragraph
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')

输出:

文件gfg.docx,可以看到回车保留在段落中

段落样式

它们用于为段落文本添加样式。您可以使用段落样式添加标题、题注、引用和标题。段落方法中有很多样式,其中一些是:

Sr. No

Style Name

Description

1.

Normal

It is used to add normal text.

2.

Body Text

It is used to add Body Text styled text.

3.

Caption

It is used to add caption styled text.

4.

Title

It is used to add Title styled text.

5.

Heading n

It is used to add Heading styled text. n can be any integer from the range 1-9.

6.

macro

It is used to add macro styled text.

7.

Quote

It is used to add quote styled text.

8.

TOC Heading

It is used to add TOC Heading styled text.

9.

Subtitle

It is used to add subtitle styled text.

10.

No Spacing

It is used to add text with no spacing.

示例 3:在 Word 文档中添加不同样式的段落的Python程序。

蟒蛇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 Normal Texted paragraph
doc.add_heading('Normal Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Normal')
  
# Adding Body Text Styled paragraph
doc.add_heading('Body Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Body Text')
  
# Adding Caption Styled paragraph
doc.add_heading('Caption Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Caption')
  
# Adding Title Styled paragraph
doc.add_heading('Title Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Title')
  
# Adding Heading Styled paragraph
doc.add_heading('Heading 1 Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Heading 1')
  
# Adding Macro Text Styled paragraph
doc.add_heading('Macro Text Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'macro')
  
# Adding Quoted Style paragraph
doc.add_heading('Quoted Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Quote')
  
# Adding TOC Heading Styled paragraph
doc.add_heading('TOC Heading Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'TOC Heading')
  
# Adding Subtitle Styled paragraph
doc.add_heading('Subtitle Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Subtitle')
  
# Adding No Spacing Styled paragraph
doc.add_heading('No Spacing Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'No Spacing')
  
# Now save the document to a location 
doc.save('gfg.docx')

输出: