In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to set up python docx pages for everyone. The quality of the article is high, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.
Word document-section-concept
Editing a word document often starts with page settings. As can be seen from the following figure, page settings often operate with margins, paper orientation and paper size. In word, they are divided into large blocks by sections, and the page settings of each section can be different. So in python-docx, the same is true. All the attributes of the current section are saved in the current section. To set the page of the current section, start by understanding the application of each attribute of the section.
If you create a blank document with python-docx, the default is one section
>>> import docx>>> doc = docx.Document()>>> len(doc.sections)1
If you want to add a section, you can do it with doc.add_section().
>>> import docx>>> doc = docx.Document()>>> len(doc.sections)1>>> doc.add_section()>>> len(doc.sections)2
Now that we know about the section, let's start setting up the section page.
set margins
Common margin attributes in python-docx package exist in the following four attributes of section:
section.top_margin: upper margin
section.bottom_margin: bottom margin
section.left_margin: left margin
section.right_margin: right margin
Margin values are measured in centimeters (cm). Let's see what the default margins are in section 2
>>> print ('up ',doc.sections[1].top_margin.cm,' down', doc.sections[1].bottom_margin.cm,'left', doc.sections[1].left_margin.cm,' right', doc.sections[1].right_margin.cm) up 2.54 down 2.54 left 3.175 right 3.175
Note that sections[1] here is section 2.
The following sets the margins to 5678 page margins commonly used in our official documents
>>> from docx.shared import Cm>>> doc.sections[1].top_margin = Cm(3.7)>>> doc.sections[1].bottom_margin = Cm(3.5)>>> doc.sections[1].left_margin = Cm(2.8)>>> doc.sections[1].right_margin = Cm(2.6)
Here, we should first import Cm, which is the unit defined in docx, and Pt, which is commonly used later, is also imported from this block.
In addition, there are three infrequently used margin attributes: binding line. Header and footer margins are respectively: section.gutter, section.header_distance, section.footer_distance. The setting method is the same as above, and will not be described again. It should be noted that the binding line also has a position attribute, which has not been found for the time being. The default is left. If the requirement is set to right, you can create a template docx document import.
Set paper orientation and size
The three properties in Section describe the page orientation and size. Section.orientation, section.page_width, section.page_height
Paper size setting unit I used to use cm, set the same page margin.
Here, we should especially talk about the orientation, that is, the paper direction, but also to set, can not say that you set the paper width wide, the height setting low paper will become horizontal, will affect printing and so on.
The value of paper orientation is set by the 2 constants enumerated in docx.enum.section.WD_ORIENTATION. They are:
WD_ORIENTATION.LANDSCAPE: Paper orientation is landscape.
WD_ORIENTATION.PORTRAIT: Paper orientation is portrait.
First check:
>>> doc.sections[0].page_height.cm27.94>>> doc.sections[0].page_width.cm21.59>>> doc.sections[0].orientation0
Default is letter size, landscape
Set to our usual A4 paper size below:
from docx.shared import Cmfrom docx.enum.section import WD_ORIENTATIONdoc.sections[0].page_height = Cm (29.7) #Set height of A4 paper doc.sections[0].page_width = Cm(21) #Set width of A4 paper doc.sections[0].orientation = WD_ORIENTATION.PORTRAIT #Set paper orientation to landscape, You can not set the default to landscape doc.sections[1].page_height = Cm(21) #Set the height of A4 paper doc.sections[1].page_width = Cm(29.7) #Set the width of A4 paper doc.sections[1].orientation = WD_ORIENTATION.LANDSCAPE #Set the paper orientation to portrait Set the columns
Sometimes we have to divide columns in some sections, and the statement to set columns for sections is relatively fixed, that is, to change a private attribute of section. First import docx.oxml.ns.qn, then do it in one sentence
from docx.oxml.ns import qndoc.sections[1]._ sectPr.xpath('./ w: cols')[0].set(qn ('w: num'), '2')#Set the second section to 2 columns
Change '2' to as many columns as you want.
About how to do python docx page settings to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.