In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Python code to seal Word documents with related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will have something to gain after reading this article on how to seal Word documents with Python code, let's take a look at it.
Implementation code:
Import osfrom docx import Documentfrom docx.enum.text import WD_ALIGN_PARAGRAPHfrom docx.shared import Pt # set the target folder path path = ".. / work / salary increase notice-exercise /" # get all the file names under the target folder file_list = os.listdir (path) for file in file_list: # splice file path file_path = path + file # Open the Word file doc = Document ( File_path) # add Paragraph object para_1 para_1 = doc.add_paragraph ('Seal:') # add Run object run_stamp run_stamp = para_1.add_run () run_stamp.add_picture (". / Shining.png") # add Paragraph object para_2 para_2 = doc.add_paragraph () # set alignment para_2. Paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT # add Run object run_comp run_comp = para_2.add_run ('Flash Technology Finance Corporation (Shining Fintech Company)') # set font run_comp.font.size = Pt (14) # font size run_comp.font.bold = True # Font bold # Save file doc.save (file_path)
The new syntax to be used in this project is shown below:
Module decomposition
Then you can open the next document, paste the text and pictures at the end of the document, and then save and close the document. Repeat this until all the documents have been processed.
Then let the property font call size, according to the above table, set the imported Pt and assign a value to size to set the font size.
The details are as follows:
From docx.shared import Pt # set font size Run object .font. size = Pt (14) # font size setting bold
The way to set the font bold is very simple and can be solved with a single line of code.
We can call bold through the property font, and then set the value to True.
The details are as follows:
# set font bold Run object .font.bold = True # font bold
Font attributes have many other functions, such as underlining fonts, deleting lines, italics, colors, specified fonts, etc., but they have nothing to do with this project, so here we briefly introduce them with pictures and operate them if you are interested.
Function block exercise
This function block is some memory knowledge, when writing code, remember to refer to the above sample code, according to the cat painting tiger can be added with the style of this function block.
From docx import Document# import control alignment required from docx.enum.text import WD_ALIGN_PARAGRAPH# import control font size required from docx.shared import Pt # set file path file_path ='. / Marbond salary increase notice .docx'# Open document doc = Document (file_path) # add paragraph 2para = doc.add_paragraph () # set alignment para.paragraph_format.alignment=WD_ALIGN_PARAGRAPH .right # add run_comprun_comp = para.add_run ("Flash Finance Corporation (Shining Finance Company)") # set font size to 14ptrun_comp.font.size=Pt (14) # set font bold run_comp.font.bold=True# save file doc.save ('. / add styled text .docx')
Running effect:
It is important to remember that font setting should be done in the Run object, and do not override it. The properties section of the Paragraph object falls in the document, and the properties of the Run object are responsible for the style of the text in the paragraph. There is a clear division of labor and each performs his own duties.
Another thing to keep in mind is:
To set the layout and layout of a paragraph, you need to use the property of the Paragraph object: paragraph_format.
To set the font style of the text, you need to use the property of the Run object: font.
This is the end of the article on "how to use Python code to seal Word documents". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Python code to seal Word documents". If you want to learn more, you are welcome to follow the industry information channel.
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.