[Python] generate Word and write data - operate Excel, Word and CSV (nanny level graphic annotation + test code + api routine)

Posted by gekko on Wed, 12 Jan 2022 01:48:40 +0100

Welcome to pay attention Python series , continuously updating
Welcome to pay attention Python series , continuously updating

API Description:

1. Create a Word routine with a title

Create Word with title
Create a blank document through Document() and call add_ The heading method creates a heading.

# Import library
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn

# New blank document
doc1 = Document()

# New document title
doc1.add_heading('Python Create a titled Word',0)

# Save file
doc1.save('Python Create a titled Word.docx')

2. Create chapters and paragraphs

add_ The heading method is used to add the article title. The second parameter is a number, indicating the title of several levels, add_ The paragraph method is used to add paragraphs to an article.

# Import library
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn

# New blank document
doc1 = Document()

doc1.add_heading('New document title',0)

doc1.add_paragraph(' Paragraph description')

doc1.add_heading('Primary title',1)

doc1.add_paragraph('Paragraph description')

doc1.add_heading('Secondary title',2)

doc1.add_paragraph('Paragraph description')

doc1.add_heading('Tertiary title',3)

doc1.add_paragraph('Paragraph description')

# Save file
doc1.save('Create chapters and paragraphs.docx')

3. Set font and reference

Add operations such as font size, bold and tilt of the article.

# @Time    : 2022/1/11 17:58
# @Author: Nanli
# @FileName: 2.3 fonts and references py
# Import library
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn
from docx.shared import RGBColor

doc1 = Document()

doc1.add_heading('New document title',0)

doc1.add_paragraph(' Paragraph description')

doc1.add_heading('Primary title',1)

doc1.add_paragraph('Paragraph description')

doc1.add_heading('Secondary title',2)

doc1.add_paragraph('Paragraph description')

doc1.add_heading('Tertiary title',3)

doc1.add_paragraph('Paragraph description')

paragraph = doc1.add_paragraph('Create paragraphs and add document content')


run = paragraph.add_run('(Note: set the font size here to 30)')#Add text to the paragraph and set the font size
run.font.size = Pt(30)

# Set English font
run = doc1.add_paragraph('Set the font here as English font:').add_run('This Font is Song typeface ')
run.font.name = 'Song typeface'

# Set Chinese font
run = doc1.add_paragraph('Set the font here as Chinese font:').add_run('The current font is Arial')
run.font.name='Song typeface'
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), 'Blackbody')

# Set italics
run = doc1.add_paragraph('Set here:').add_run('The text is italicized ')
run.italic = True

# Set bold
run = doc1.add_paragraph('Set here:').add_run('Set the font here to bold').bold = True

# Set font underline
run = doc1.add_paragraph('Underline here:').add_run('Set the font here to underlined').underline = True

# Set font color
run = doc1.add_paragraph('The font here is red:').add_run('Set the font here to red')
run.font.color.rgb = RGBColor(0xFF, 0x00, 0x00)

# Add reference
doc1.add_paragraph('Life is short, I use it Python', style='Intense Quote')

# Save file
doc1.save('Fonts and references.docx')

4. Create project list

Display the contents in a list

# @Time    : 2022/1/11 18:05
# @Author: Nanli
# @FileName: 2.4 item list py
# Import library
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn

# New document
doc1 = Document()

doc1.add_paragraph('Learning knowledge:')

# Add unordered list
doc1.add_paragraph(
    'c language', style='List Bullet'
)
doc1.add_paragraph(
    'java', style='List Bullet'
)
doc1.add_paragraph(
    'python', style='List Bullet'
)
doc1.add_paragraph(
    'lua', style='List Bullet'
)
doc1.add_paragraph(
    'html', style='List Bullet'
)

doc1.add_paragraph('2022 Year Outlook:')
# Add ordered list
doc1.add_paragraph(
    'CSDN Fan 1000', style='List Number'
)
doc1.add_paragraph(
    'Lose 10 kg', style='List Number'
)

doc1.add_paragraph(
    'Finish the machine vision project', style='List Number'
)
doc1.add_paragraph(
    'Finish the robot dance', style='List Number'
)
doc1.add_paragraph(
    'Single chip microcomputer wireless positioning', style='List Number'
)

# Save file
doc1.save('Item list.docx')

5. Pictures and tables

Any picture named
A picture of you walking away png (that's my head)

Using add_picture add picture; Using add_table adds a table.

# @Time    : 2022/1/11 18:53
# @Author: Nanli
# @FileName: 2.5 pictures and tables py
# Import library
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn

# New document
doc1 = Document()

doc1.add_paragraph('Learning knowledge:')

# Add unordered list
doc1.add_paragraph(
    'c language', style='List Bullet'
)
doc1.add_paragraph(
    'java', style='List Bullet'
)
doc1.add_paragraph(
    'python', style='List Bullet'
)
doc1.add_paragraph(
    'lua', style='List Bullet'
)
doc1.add_paragraph(
    'html', style='List Bullet'
)

doc1.add_paragraph('2022 Year Outlook:')
# Add ordered list
doc1.add_paragraph(
    'CSDN Fan 1000', style='List Number'
)
doc1.add_paragraph(
    'Lose 10 kg', style='List Number'
)

doc1.add_paragraph(
    'Finish the machine vision project', style='List Number'
)
doc1.add_paragraph(
    'Finish the robot dance', style='List Number'
)
doc1.add_paragraph(
    'Single chip microcomputer wireless positioning', style='List Number'
)
doc1.add_heading('picture', 2)

# Add image
doc1.add_picture('The picture of you walking away.png', width=Inches(5.5))

doc1.add_heading('form', 2)

# Add table, this is the table header
table = doc1.add_table(rows=1, cols=4)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'number'
hdr_cells[1].text = 'full name'
hdr_cells[2].text = 'occupation'

# This is tabular data
records = (
    (1, 'Zhao Yun', 'Wild father'),
    (2, 'army officer's hat ornaments', 'Buddha'),
    (3, 'Luban', 'Marksman'),
    (4, 'Crazy iron', 'Tongtianbian'),
    (5, 'I found you far away', 'Bloggers who don't get praise')
)

# Traverse the data and display
for id, name, work in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(id)
    row_cells[1].text = name
    row_cells[2].text = work

# Add paging manually
doc1.add_page_break()

# Save file
doc1.save('Pictures and tables.docx')

summary

If you like, give me a 👍, Pay attention! Continue to share with you the problems encountered in the process of typing code!

Welcome to pay attention Python series , continuously updating
Welcome to pay attention Python series , continuously updating
[write content to generate Excel - operate Excel, Word and CSV (1) (nanny level graphic annotation + test code + api routine)]
[read Excel content - operate Excel, Word and CSV (2) (nanny level graphic annotation + test code + api routine)]
[modify Excel content - operate Excel, Word and CSV (3) (nanny level graphic annotation + test code + api routine)]
[excel format conversion operation - operation of Excel, Word and CSV (4) (nanny level graphic notes + test code + api routine)]
[generate Word and write data - operate Excel, Word and CSV (5) (nanny level graphic annotation + test code + api routine)]
[read Word to obtain data - operate Excel, Word and CSV (6) (nanny level graphic annotation + test code + api routine)]
[write and read CSV - operation Excel, Word, CSV (7) (nanny level text and text notes + test code + api routine)]
[more details]

All files have been uploaded to the code cloud

https://gitee.com/miao-zehao/python_to_-excel-and-word-and-csv/tree/master/


Topics: Python Back-end Data Analysis