“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

This blog is about docx documentation in Python. The main content involved is Python-Docx, a third-party library that can manipulate Word documents (docX only).

Writing in the front

Python-docx does not support doc documents. If you use doc documents, you need to convert them to DOCX format with Word software in advance. Doc and DOCx are fundamentally different, one is binary, the other is XML file.

PIP install python-docx

The following urls should be prepared first

  1. The official handbook: python – docx. Readthedocs. IO/en/latest/I…
  2. Pypi official address: pypi.org/project/pyt…

Data ready, can enter the coding link.

Create a document

The first requirement is to create a document and write various information into it.

To implement the first step, write a heading

Note the use of the add_heading() function below.

from docx import Document

Create a Document object, equivalent to opening a Word Document on your computer
document = Document()

Add a title to your Word document
document.add_heading('This is a title.',level=0)

# save document
document.save('demo.docx')
Copy the code

Add text paragraph

Note below that the add_Paragraph () function adds paragraphs and appends text with add_run()

from docx import Document

Create a Document object, equivalent to opening a Word Document on your computer
document = Document()

Add a title to your Word document
document.add_heading('This is a title.', level=0)

Add paragraphs to the document
p = document.add_paragraph('This is a free passage.')
# Add styled text
# Add paragraph, text can contain TAB (\t), newline (\n), carriage return (\r), etc
# add_run() appends text to the end of the paragraph
p.add_run('\n I am tilted ').italic = True  Add a slanted text
p.add_run('\n I made it bold ').bold = True  Add a bold text

# save document
document.save('demo.docx')
Copy the code

Add text before paragraphs

p.add_run('\n I am tilted ').italic = True  Add a slanted text
p.add_run('\n I made it bold ').bold = True  Add a bold text

# Insert content before paragraph
prep_p = p.insert_paragraph_before('Insert content before paragraph')
Copy the code

Paragraphs can also add page breaks like this:

# Insert content before paragraph
prep_p = p.insert_paragraph_before('Insert content before paragraph')

document.add_page_break()
p1 = document.add_paragraph('New page, new paragraph')
Copy the code

Adding a list

Add an unordered list

# Add list (front dot)
document.add_paragraph('Dot in front of me.', style='List Bullet')
# Add list (front dot)
document.add_paragraph('Second dot', style='List Bullet')
Copy the code

The addition of ordered lists

# Add list (first number)
document.add_paragraph('I have numbers in front of me.', style='List Number')
# Add list (first number)
document.add_paragraph('Second number', style='List Number')
Copy the code

Adding pictures

The Document object has the following methods:

  • add_heading(self, text='', level=1): Add title;
  • add_page_break(self): page break;
  • add_paragraph(self, text='', style=None): Add paragraph;
  • add_picture(self, image_path_or_stream, width=None, height=None): Add pictures;
  • add_section(self, start_type=2): Add section;
  • add_table(self, rows, cols, style=None): Add table;

This section focuses on the add_picture() method.

document.add_picture(r".. /9.png", width=Inches(1))
Copy the code

The image_path_or_stream parameter in the add_picture() function can be a relative or absolute address, as well as an image data stream. As Inches, python-docx provides centimeters. If you set 1 Cm: Cm(1), import from docx.shared import Inches, Cm in advance.

A table

You can add tables to a Word document using the add_table() function.

p1 = document.add_paragraph('New page, new paragraph')

Add a 2×2 table
table = document.add_table(rows=2, cols=2)
Get the cell in row 1, column 2
cell = table.cell(0.1)

Set cell text
cell.text = 'Row 1 column 2'

Get line 2
row = table.rows[1]
row.cells[0].text = 'Eraser'
row.cells[1].text = 'Joe yu'
Copy the code

Pay attention to the relevant notes when learning, you can master this part of the content.

Related style Settings

Add_run () is used to format the paragraph. The paragraph generated by add_Paragraph () cannot be formatted directly.

# Import the module
from docx.shared import Pt, Cm, Inches
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import RGBColor
Level =0
head0 = document.add_heading(level=0)
# center the title
head0.alignment = WD_ALIGN_PARAGRAPH.CENTER
title_run = head0.add_run('This is a middle title.', )
title_run.font.size = Pt(24)
# Title English font
title_run.font.name = 'Times New Roman'
# Title Chinese font
title_run.element.rPr.rFonts.set(qn('w:eastAsia'), Microsoft Yahei)
# font color
title_run.font.color.rgb = RGBColor(4.60.169)
Copy the code

Where the WD_ALIGN_PARAGRAPH object represents the way a paragraph treats it, and its value includes the following

  • WD_ALIGN_PARAGRAPH. LEFT: left aligned;
  • WD_ALIGN_PARAGRAPH. CENTER: centered against it;
  • WD_ALIGN_PARAGRAPH. RIGHT: right aligned;
  • WD_ALIGN_PARAGRAPH. JUSTIFY: aligned at both ends;

The line_spacing property sets the line spacing, space_before for the front and space_after for the back.

Italic, underscore, bold Settings

font.italic = True  Set italics
font.underline = True  # set underline
font.bold = True  # Set bold
Copy the code

The first line is indent, paragraph_format_line_indent you can set an indent value.

Add_style (‘textstyle’, WD_STYLE_TYPE. PARAGRAPH), using the add_style() method. You can then use the provided properties to set the font to font. Size and font color to font.

This property can then be applied when adding paragraphs.


from docx.shared import RGBColor
from docx.enum.style import WD_STYLE_TYPE
style = document.styles.add_style('textstyle', WD_STYLE_TYPE.PARAGRAPH)
# font size
style.font.size = Pt(16)
# font color
style.font.color.rgb = RGBColor(66.100.0)
p1 = document.add_paragraph(God man eraser,style=style)
Copy the code

Header and footer

The header and footer can be set directly, with the reference code shown below

header = document.sections[0].header
header.add_paragraph('Eraser header')
Copy the code

The footer Settings

footer = document.sections[0].footer
Copy the code

Today is day 277/365 of continuous writing. You can follow me, like me, comment on me, favorites me.