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/03 Report--
This article mainly explains "what are the necessary tools for python-docx to deal with Word". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the necessary tools for python-docx to deal with Word".
My understanding
Why to use python-docx, because recently downloaded a large number of web articles, but the format is html, I am used to using word to deal with words, so I try to convert html documents into word, the first question to consider is how to store the text extracted from html in word, before using pandoc direct conversion, the effect after conversion is not ideal, there is no format Do not meet the needs of people with obsessive-compulsive disorder who have strict requirements for word format, so I search everywhere for other methods, and finally live up to my efforts. After a few days of studying python-docx, I feel very suitable for me. While analyzing html documents, I think about how to use python-docx to save the desired format word, because my word typesetting, generally used to set the page to 5678 margins, size A4, text main title founder small standard Song Other headings are either bold or bold, the first line of the text should be indented by 2 characters imitating Song _ GB2312, and the footer should be displayed with page numbers, probably in these styles.
Installation library: from docx import Document (document read and write) library used by pip install python-docx (font size, not necessarily all) from docx.oxml.ns import qn (font formatting, column, etc.) from docx.shared import RGBColor (setting font color) from docx.enum.text import WD_ALIGN_PARAGRAPH (setting its way) from docx.enum.section import WD_ORIENTATION (paper direction is used) general formatting
One of the advantages of this setting is that when you write the document into it, you will automatically press this format, and if you need to change it, you will change it separately.
Docment = docx.Document (docx_tamplate) # read template documents. Template documents can not be used here, because python-docx cannot set page numbers So I first built a blank document with a page number as a template document # set the default format of the text # font size three characters (16) docment.styles ['Normal']. Font.size = Pt (16) # font imitation Song _ GB2312docment.styles [' Normal']. Font.name = u 'imitation Song _ GB2312'docment.styles [' Normal']. _ element.rPr.rFonts.set (qn ('wrapeastAsia')) U' imitating Song _ GB2312') # before the line spacing of 28 lbs, Leave no space after the segment docment.styles ['Normal']. Paragraph_format.line_spacing = Pt (29) docment.styles [' Normal']. Paragraph_format.space_before = Pt (0) docment.styles ['Normal']. Paragraph_format.space_after = Pt (0) # first line indented 2 characters docment.styles [' Normal']. Paragraph_format.first_line_indent = 40640 turn off the orphan control docment.styles ['Normal'] .paragraph_format.widow_ Control = False# set page size docment.sections [0] .page _ height = Cm (29.7) # set the height of A4 paper docment.sections [0] .page _ width = Cm (21) # set the width of A4 paper # set margins docment.sections [0] .top _ margin = Cm (3.7) docment.sections [0] .bottom _ margin = Cm (3.4) docment.sections [0] .left _ margin = Cm Docment.sections [0] .right _ margin = Cm (2.6) set segment format separately doc=Document () # create a blank document p1=doc.add_paragraph () # initialize create a natural segment p1.alignment=WD_ALIGN_PARAGRAPH.CENTER # align to center No this sentence defaults to left alignment. Also right alignment: RIGHT, both ends alignment: JUSTIFY, scatter alignment: DISTRIBUTE p1.paragraph_format.line_spacing=1.5 # set the paragraph with 1.5x line spacing, or you can use Pt units to set p1.paragraph_format.first_line_indent=Inches (0.5) # paragraph indentation as the default value above I am still used to setting the 2-character value to: 406400p1.paragraph_format.left_line_indent=Inches (0.5) # set left indent 0.5 inch. Generally, you don't need p1.paragraph_format.right_line_indent=Inches (0.5) # to set right indent 0.5 inch. Generally, it is not necessary to use p1.paragraph_format.keep_together = False # paging before paragraph p1.paragraph_format.keep_with_next = False # on the same page as the next paragraph p1.paragraph_format.page_break_before = True # paragraph without paging p1.paragraph_format.widow_control = False # isolated control p1.space_after=Pt (5) # set the post-segment distance to 5 pounds p1.space_before=Pt (5) # set the pre-paragraph distance to 5 Pound run1=p1.add_run ('Hello') # the text "Hello" run1.font.size=Pt (12) # separately set the font size to 24run1.font.bold=True # style setting bold run1.italic=True # glyph setting italic run1.font.underline = True # underline run1.font.color.rgb = RGBColor 0,0) # Color insert picture # add picture Set picture size doc.add_picture (r "picture path", width=Cm (10)) insert table tab = doc.add_table (rows=5, cols=8, style='Table Grid') # create a table with 5 rows and 8 columns The style is Table Gridtab.cell (0,0). Text = 'table corner' # 0 row 0 column content is table corner cell=tab.cell (0,1) .merge (tab.cell (0,3)) # merge 0 row 1 column to 0 row 3 column p = cell.paragraphs [0] run = p.add_run ('merge') # create a paragraph in the cell content and write the 'merge' text run.font.size = Pt # font size setting Corresponding to the font size in word, the number 5 word run.bold = Truep.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # is set to bold and centered to show thank you for your reading. the above is the content of "what are the necessary tools for python-docx to deal with Word". After the study of this article, I believe you have a deeper understanding of what are the necessary tools for python-docx to deal with Word. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.