Teach you how to use Python to manipulate word documents

Keywords: Python Windows jupyter Programming

Use Python to manipulate word documents

Installation of Python-docx

Python-docx is a module specially for word documents. It can only read docx but not doc files. To put it bluntly, Python is equivalent to the windows operating system. QQ is the software running on the windows operating system. The biggest function of QQ is to chat and talk. Here python-docx is equivalent to QQ here. Its main function is to operate and manage docx files. (Personal understanding, non-professional!)

1.1. Install Python-docx

1.1.1. Install python-docx using a virtual environment

pip install python-docx # Installation command

After installation, run Jupyter notebook in this virtual environment

jupyter notebook

1.1.2, Switch the working directory (using the% cd command)

% cd F:python_test1Python_office# into the directory
 % pwd # View the current working directory
 >'F: python_test1 Python_office' Output
 What I don't know in the process of learning can be added to me?
python learning communication deduction qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn

Editing word Documents

Before editing the word document, you need to import the Document module as follows:

from docx import Document
Doc = Document() 
Interpretation: from the docx file, import a thing called Document. Document means document, so it is a thing to operate on word documents. Doc = Document() below can be understood as Document is a class. This operation is the process of instantiation, and the generated object is: Doc = Document().

Document instantiates an Object called Doc. Doc certainly has many ways to manipulate word documents. These ways are as follows:

Doc.add_heading("Python What is it?")
<docx.text.paragraph.Paragraph at 0x28033582e48>
# Doc.add_heading means adding something called heading. Heading here means heading, which means adding a heading called python. It generates an object when the run is complete.

2.1. Add a paragraph

Doc.add_paragraph("Python Is an object-oriented programming language~~~") # Paragraph here means a paragraph.
<docx.text.paragraph.Paragraph at 0x280335a17b8>

2.2. Add more than one paragraph

Doc.add_paragraph("Python ")
Doc.add_paragraph("Python Yes word Operation")

2.3. How to view word documents?

After we have finished writing, we need to look at the first need to save as follows:

Doc.save("Python_word.docx")
# save: save as a file named: Python_word.docx

Third, how to add a first-level heading, a second-level heading, a third-level heading. ?

3.1. Add Title 1

Doc = Document()
Doc.add_heading("This is the Title I.",level=1)
Doc.add_heading("This is the secondary title.",level=2)
Doc.add_heading("This is the third title.",level=3)
Doc.add_heading("This is Title 4.",level=4)
Doc.save("Title.docx")

The opening effect is as follows:

How to add a subheading?

Doc.add_heading("This is a first-level title.",level=1)
Doc.add_paragraph("This is a subtitle.","Subtitle")
Doc.save("Subheading.docx")

5. Look at existing styles

5.1. See what styles paragraph s have

What I don't know in the process of learning can be added to me?
python Learning Exchange Button qun,784758214
//There are good learning video tutorials, development tools and e-books in the group.
//Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
from docx.enum.style import WD_STYLE_TYPE
for i in Doc.styles:
 if i.type == WD_STYLE_TYPE.PARAGRAPH:
 print(i.name)
>>>
Normal
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Heading 7
Heading 8
Heading 9
No Spacing
Title
Subtitle
List Paragraph
Body Text
Body Text 2
Body Text 3
List
List 2
List 3
List Bullet
List Bullet 2
List Bullet 3
List Number
List Number 2
List Number 3
List Continue
List Continue 2
List Continue 3
macro
Quote
Caption
Intense Quote
TOC Heading

5.2. Look at the styles of the text

from docx.enum.style import WD_STYLE_TYPE
for i in Doc.styles:
 if i.type == WD_STYLE_TYPE.CHARACTER:
 print(i.name)
>>>
Default Paragraph Font
Heading 1 Char
Heading 2 Char
Heading 3 Char
Title Char
Subtitle Char
Body Text Char
Body Text 2 Char
Body Text 3 Char
Macro Text Char
Quote Char
Heading 4 Char
Heading 5 Char
Heading 6 Char
Heading 7 Char
Heading 8 Char
Heading 9 Char
Strong
Emphasis
Intense Quote Char
Subtle Emphasis
Intense Emphasis
Subtle Reference
Intense Reference
Book Title

Case study:

from docx import Document
%cd D:YanZan_python2018word
Docx = Document()
Docx.add_heading("This is a first-level title.",level=1)
Docx.add_paragraph("This is a subheading.","Title")
A = Docx.add_paragraph("My name is aaa")
A.add_run("I am happy to learn, aha ha ha ha ha, very good Good!!!")
Docx.add_heading("This is a secondary title.",level=2)
A = Docx.add_paragraph("This is the content of the secondary title.")
B = A.add_run("Continue to add the text in the second title!!!!!!!")
B.font.bold = True # At the same time, I want to bold these texts.~~~~
B.font.size = (20)
Docx.add_heading("I love learning. Python The following is python Of logo ah",level=3)
Docx.add_picture("1.png")
Docx.add_table(rows=5, cols=5)
Docx.save("Python.docx")

If you are still confused in the world of programming, you can join our Python Learning button qun: 784758214 to see how our predecessors learned. From basic Python script to web development, crawler, django, data mining, zero-base to actual project data are sorted out. To every Python buddy! Every day, share some learning methods and small details that need attention, and share technical experience! Click to join us python learner gathering place

Posted by Benny007 on Wed, 18 Sep 2019 07:07:32 -0700