In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use Python to automatically generate Word documents, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Use the following command to install:
Pip install python-docx
The basic steps to use the library are:
1. Create a document object (either automatically using the default template or using an existing file).
two。 Format the document (default font, page margins, and so on).
3. Add paragraph text, tables, images, and so on to the document object and specify its style.
4. Save the document.
Note: this library only supports the generation of later versions of Word2007, that is, documents with a .docx extension.
Here is a step-by-step description of its basic usage:
Step 1:
From docx import Documentdoc = Document () # create the document object with the default template doc = Document ('a.docx') # read the a.docx document and establish the document object
Step 2:
From docx.shared import Inches,Ptdef chg_font (obj,fontname=' Microsoft Yahei, size=None): # # set the font function obj.font.name = fontname obj._element.rPr.rFonts.set (qn ('wrapper eastAsia`), fontname) if size and isinstance (size Pt): obj.font.size = sizedistance = Inches (0.3) sec = doc.sections [0] # sections in the corresponding document "section" sec.left_margin = distance # set left, right, top, and Bottom page margin sec.right_margin = distancesec.top_margin = distancesec.bottom_margin = distancesec.page_width = Inches (12) # set page width sec.page_height = Inches (20) # set page height # # set default font chg_font (doc.styles ['Normal'] Fontname=' Arial')
Step 3:
1. Add paragraph text
Paragraph = doc.add_paragraph ('text....') ph_format = paragraph.paragraph_formatph_format.space_before = Pt (10) # set the spacing before segments ph_format.space_after = Pt (12) # set the spacing after segments ph_format.line_spacing=Pt (19) # set the row spacing
If you want the text in the same paragraph to be formatted differently, you need to use the Run object (which can be understood as an intra-paragraph object that can be formatted separately).
Such as:
Run = paragraph.add_run ('text...') run.bold = True # set font to bold chg_font (run,fontname=' Microsoft Yaha', size=Pt (12)) # set font and font size
two。 Add a table and write about it
Tab = doc.add_table (rows=4,cols=4) # add a 4-row and 4-column empty table cell=tab.cell (1Pol 3) # get a cell object (index from 0)
Add text to the cell:
Cell.text='abc'
Add multiline text to the cell (specify line wrapping)
Ph = cell.paragraphs [0] run=ph.add_run ('text....')' run.add_break () # add a broken line run.add_picture ('a.png') # to insert an image, which can be an image in memory, and width=Inches (1.0) specifies the width.
3. Add an image to the document:
Doc.add_picture ('a.png')
Step 4:
Doc.save ('a.docx') # Save the image
To do a simple test, the speed of generating documents is relatively fast. It feels much faster than using the reportlab library to generate PDF documents.
The above is all the contents of the article "how to use Python to automatically generate Word documents". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.