The installationPython-docx

Docx is not a Python standard library. It is a third-party extension. We install it using the PIP command:

pip install python-docx
Copy the code

If the PIP cannot be installed due to network or other reasons

Please visit the pypi.org/project/pyt… Manually download the compressed file, decompress it, and install it

tar xvzf python-docx-{version}.tar.gz
cd python-docx-{version}
python setup.py install
Copy the code

Quick start

New document

from docx import Document
document = Document()
Copy the code

This step is very simple. We declare a Document object, and if we don’t pass in a DOCX Document path, by default a new blank Document opens

This way of using default parameters to call up the document object greatly simplifies the amount of code we have, making it much simpler

Save the document

from docx import Document

document = Document()
document.save("./nihao.docx")
Copy the code

The save method is used to save docX documents

Try to write the title and content

Add the title

from docx import Document
document = Document()
document.add_heading('Underground Station')
document.add_heading(Ding Xiang Building, level=2)
# document.save("./ underground station.docx")
Copy the code

Go to the source code and see the add_heading method

def add_heading(self, text="", level=1) :
    """Return a heading paragraph newly added to the end of the document. The heading paragraph will contain *text* and have  its paragraph style determined by *level*. If *level* is 0, the style is set to `Title`. If *level* is 1 (or omitted), `Heading 1` is used. Otherwise the style is set to `Heading {level}`. Raises |ValueError| if *level* is outside the range 0-9. """
    if not 0 <= level <= 9:
        raise ValueError("level must be in range 0-9, got %d" % level)
    style = "Title" if level == 0 else "Heading %d" % level
    return self.add_paragraph(text, style)
Copy the code

As you can see, the add_heading method receives two parameters. The text parameter is the heading text and the level is the level size. The default is the level 1 heading

Add a paragraph

A paragraph is the most important block-level object in a DOCX document and is used to write bodies, images, tables, and so on

paragraph = document.add_paragraph('Jia GUI was originally a local ruffian in Anqiu.')
Copy the code

Using the add_paragraph method we can write the content of the paragraph, but more often than not we want to style the content and customize the style.

Python-docx supports most of the text styles of native Docx, such as alignment, indentation, line spacing, font size, font style, color, and so on

For example, we’ve prepared a list of quotes from underground transit stations

I put my face up my ass and I started to fight with him. Twenty years later, I am a hero again... Rape. I'm not fucking hitting anyone today. I'm fucking hitting you today. A rogue two bandits Japanese military police, guard detective team biological son maintenance. Cattle were needed to build the Promised Land, and even more so to maintain the new order. The imperial army and cattle could not be separated. The imperial army will regard cattle as their brothers. I knew that girl was gonna look good. You drop the knife big drop ok, I drop, here you go. Your desk is big. I work here. Your wife is so pretty, I... What a fool as a traitor. Yeah, yeah, yeah, YEAH. I'm that captain shit.Copy the code

We put it in a body paragraph and embellished it

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt, RGBColor
from docx.oxml.ns import qn

document = Document()


# Globally specify font
document.styles['Normal'].font.name = U '. Ping Fang - Jane '
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), U '. Ping Fang - Jane ')

header = document.add_heading('Underground Station',level=2)

Set the title alignment to center
header_format = header.paragraph_format
header_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

text = "" I covered my face and pouted my butt and started to fight with him. Twenty years later, I am a hero again... Rape. I'm not fucking hitting anyone today. I'm fucking hitting you today. A rogue two bandits Japanese military police, guard detective team biological son maintenance. Cattle were needed to build the Promised Land, and even more so to maintain the new order. The imperial army and cattle could not be separated. The imperial army will regard cattle as their brothers. I knew that girl was gonna look good. You drop the knife big drop ok, I drop, here you go. Your desk is big. I work here. Your wife is so pretty, I... What a fool as a traitor. Yeah, yeah, yeah, YEAH. I'm that captain shit. ' ' '

Declare a paragraph
paragraph = document.add_paragraph()

Set alignment to center
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

Set a block object
run = paragraph.add_run(text)

# Set font size and color
run.font.size = Pt(7)
run.font.color.rgb = RGBColor(0x42.0x24.0xE9) # RGB 

document.save("./ Underground station.docx")
Copy the code

What did

  • indocumentObject with a global font set,Note: If yesdocxNo built-in font style, you need to download your own font to the local for selection
  • Set up aThe secondary title
  • throughparagraph_formatProperty to set the alignment
  • throughadd_runAdd block elements and insert prepared text
  • Sets the font size and color for the body content
  • Save the document

The document looks like this

Insert the picture

from docx.shared import Inches
document.add_picture('./ The Beautiful Face of Captain Jia. PNG ', width=Inches(1.0))
Copy the code

Add a local image to the document using the add_picture method. The default DOCx library does not support parsing and adding online image addresses.

So if we want to add an online image, we can get the binary stream of the image and add it to the document using the add_picture method

import requests,io
from docx.shared import Inches
url = 'https://www.easyicon.net/api/resizeApi.php?id=1311353&size=128'
io_url = io.BytesIO(requests.get(url).content)
# Add an image
document.add_picture(io_url, width=Inches(1.0))
Copy the code

Insert table

It is possible to insert and manipulate tables in docX documents

table = document.add_table(rows=2, cols=2)
Copy the code

We added a table using the add_TABLE method, and we tried to manipulate the cells with some properties and methods of the table

cell = table.cell(0.1)
cell.text = 'Hey, there's a fight. Hey.'
Copy the code

The.text property can assign (assign or modify) a cell

  • 0That’s the first row
  • 1The second column

Here the table’s row and column indexes start at 0

Specifies the cell in which rows are written, and the rows attribute specifies a row or rows. The cells are then assigned through the cells attribute of the row

row = table.rows[2]
row.cells[0].text = 'Our house blew up the other day during the heat.'
row.cells[1].text = 'Well, turn on the air conditioner.'
Copy the code
  • 2Indicates that the column in the table is specified3
  • 0Indicates that the end of the row was written1
  • 1Indicates that the end of the row was written2

Gets the total number of rows and columns in the table through the len function

row_count = len(table.rows)
col_count = len(table.columns)
Copy the code

Step by step add rows and columns

When we cannot determine the number of rows in the current document table, we can choose to add rows or columns in real time, so that we can flexibly control the length and width of the table, reducing unnecessary empty rows or columns

For example, if you don’t know the length of the data list or need to add rows in real time, refer to the following code

items = [
    {"name":"Gu Gui"."desc":"Captain of the Detective Team"."createDate":"2021-04-20"},
    {"name":"Gold standard"."desc":"Captain of the Guard."."createDate":"2021-04-21"},
    {"name":"Black cane"."desc":"Chief of the Secret Service."."createDate":"2021-04-22"},
    {"name":"Sun Youfu"."desc":"The owner of the Ding Xiang Building"."createDate":"2021-04-23"},
    {"name":"Water root"."desc":"Ding Xiang's big buddy"."createDate":"2021-04-24"}]# add table
table = document.add_table(1.3)
table.style='Medium Grid 1 Accent 1'

# header
heading_cells = table.rows[0].cells
heading_cells[0].text = 'name'
heading_cells[1].text = 'job'
heading_cells[2].text = 'Creation time'

for item in items:
    cells = table.add_row().cells
    cells[0].text = item["name"]
    cells[1].text = item["desc"]
    cells[2].text = item["createDate"]
Copy the code
  • I added a13Column of the table as addedheaderwith
  • Add a little style to the tableMedium Grid 1 Accent 1
  • useadd_rowMethod to dynamically add row operations

About table style list you can refer to www.cnblogs.com/AbnerLc/p/1…

We can also use len(items[0]) to get the length of the dictionary in the items list as the length of the column

# add table
table = document.add_table(1.len(items[0]))
Copy the code

Headers and footers

Sometimes we add headers and footnotes to documents

Add the header

document = Document()
section = document.sections[0]
header = section.header
paragraph = header.paragraphs[0]
paragraph.text = Ding Xiang Building
# Paragraph. Text = "left-align text \t center text \t right align text"
paragraph.style = document.styles["Header"]
Copy the code

Declare a section and use the Header property to add a header to the document

Add a footer

footer = section.footer
paragraph = footer.paragraphs[0]
# Center display
paragraph.text = "\t from underground station \t"
Copy the code

Simply replace the header with a footer to add a footer to the document

This is just a list of some of the common features of the Python-docx library. For more details on document styles and usage, you can use the official python-docx document python-docx.readthedocs.io

Wish you have a good mood!