📜  使用页面方向和分页属性Python .docx 模块

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

使用页面方向和分页属性Python .docx 模块

先决条件:使用 .docx 模块

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

pip install python-docx

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

页面方向

为了改变 word 文档的方向,我们使用docx.enum.section模块的 WD_ORIENT 。为了调用或设置截面的方向,我们将使用截面类的方向方法。

句法:

section.orientation = WD_ORIENT.[Orientation Type]

有两种可能的方向。

Sr. No.

Orientation Type

Description

1.

Portrait

It is used to set the orientation to portrait.

2.

Landscape

It is used to set the orientation to landscape.

笔记:

  • 纵向是默认方向。
  • 方向方法只能用于部分,因此要使用一种方法,您必须先选择 Word 文档的一个部分。

示例 1:打印 Word 文档的默认方向。

Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Selecting a section of the document
section = doc.sections[0]
 
# Printing the default orientation.
print("Default Orientation:", section.orientation)


Python3
# Import docx NOT python-docx
import docx
from docx.enum.section import WD_ORIENT
 
# Create an instance of a word document
doc = docx.Document()
 
# Selecting a section of the document
section = doc.sections[0]
 
# Printing the default orientation.
print("Default Orientation:",
      section.orientation)
 
# Changing the orientation to landscape
section.orientation = WD_ORIENT.LANDSCAPE
 
# Printing the new orientation.
print("New Orientation:",
      section.orientation)


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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting keep_together as True
para.keep_together = True
 
# 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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting keep_with_next as True
para.keep_with_next = True
 
# 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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting page_break_before as True
para.page_break_before = True
 
# 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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting widow_control as True
para.widow_control = True
 
# Now save the document to a location
doc.save('gfg.docx')



输出:

Default Orientation: PORTRAIT (0)

示例 2:将方向从默认更改为横向。

蟒蛇3

# Import docx NOT python-docx
import docx
from docx.enum.section import WD_ORIENT
 
# Create an instance of a word document
doc = docx.Document()
 
# Selecting a section of the document
section = doc.sections[0]
 
# Printing the default orientation.
print("Default Orientation:",
      section.orientation)
 
# Changing the orientation to landscape
section.orientation = WD_ORIENT.LANDSCAPE
 
# Printing the new orientation.
print("New Orientation:",
      section.orientation)


输出:

Default Orientation: PORTRAIT (0)
New Orientation: LANDSCAPE (1)

分页属性

分页属性是控制页面边界附近段落行为的属性或样式。 .docx 模块中有四种样式的 add_paragraph()函数属于此类别。

句法:

doc.add_paragraph(String s, style=None)

参数:

  • String s:作为段落添加的字符串数据。该字符串可以包含字符“ \n ”、制表符“ \t ”或回车字符“ \r ”。
  • 样式:用于设置样式。

分页属性下的样式是:

Sr. NO.

Style Name

Description

1.

keep_together

Keeps the content of the paragraph on one page.

2.

keep_with_next

Keeps the content of a paragraph with another subsequent paragraph.

3.

page_break_before

Move a paragraph to a new page because of a page break.

4.

widow_control

Keeps the first and last line of the paragraph together with the rest of the paragraph. 

注意:所有四种样式都可以设置为 true、false 或 none。 True表示“on”, False表示“off”, None表示该属性是从样式层次结构继承的。

示例 3:在 Word 文档中的段落上使用 keep_together。

蟒蛇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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting keep_together as True
para.keep_together = True
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 4:在 Word 文档中的段落上使用 keep_with_next。

蟒蛇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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting keep_with_next as True
para.keep_with_next = True
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 5:在 Word 文档中的段落上使用 page_break_before。

蟒蛇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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting page_break_before as True
para.page_break_before = True
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 6:对 Word 文档中的段落使用 widow_control。

蟒蛇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
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
 
# Setting widow_control as True
para.widow_control = True
 
# Now save the document to a location
doc.save('gfg.docx')


输出: