Using word to teach you how to operate Python project

Time:2020-10-17

In recent work, it is necessary to visualize the query data for a period of time and export it to word format. Since I am not familiar with word operation, I have consulted the relevant documents. Here I briefly record how to use Python to operate word.

In addition, I would like to recommend a full range of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details

explain

  • This code comes from the official document( https://python-docx.readthedocs.io/en/latest/ )Here are some comments on the code.
  • This code records the common operations of word:
    1. Add a title
    2. Add a paragraph
    3. Style paragraph text
    4. Insert picture
    5. Add form and input content
    6. Add Pagination

design sketch

 

code

# coding:utf-8

"""
    Python operation word
"""

from docx import Document
from docx.shared import Inches

#Create word document object
document = Document()
#Add title
document.add_heading('Document Title', 0)

#Add paragraph
p = document.add_paragraph('A plain paragraph having some ')

#Add paragraph文字,并指定样式:这里设置文字为加粗
p.add_run('bold').bold = True

#Add paragraph文字
p.add_run(' and some ')

#Add paragraph文字,并指定样式:设置文字为斜体
p.add_run('italic.').italic = True

#Add first level title
document.add_heading('Heading, level 1', level=1)

#Add paragraph,并设置段落样式
document.add_paragraph('Intense quote', style='Intense Quote')

document.add_paragraph(
    'first item in unordered list', style: 'list bullet' ා
)
document.add_paragraph(
    'first item in ordered list', style: 'list number' ා
)

#Insert a picture: inches is the unit of the picture
document.add_picture('pic.jpg', width=Inches(3.0))

records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)

#Create a table with one row and three columns
table = document.add_table(rows=1, cols=3)
#Gets the number of all columns in the first row
hdr_cells = table.rows[0].cells

#Add content to the columns in the first row
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'

#Add new rows to the table table and add content to the columns
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc

#Add Pagination
document.add_page_break()

#Save the world document
document.save('demo.docx')

if __name__ == "__main__":
    pass

Like a like! In addition, I would like to recommend a full range of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details

The text and pictures of this article are from the Internet and my own ideas. They are for study and communication only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.