2022-03-16 02:00:01 +00:00
|
|
|
Python docx =
|
2022-03-15 21:30:01 +00:00
|
|
|
|
|
|
|
Python docx is a library for generating a .docx file with python code
|
|
|
|
|
|
|
|
== Setup ==
|
|
|
|
|
|
|
|
{{{
|
|
|
|
from docx import Document
|
|
|
|
from docx.shared import Inches
|
|
|
|
}}}
|
|
|
|
|
|
|
|
== Metadata ==
|
|
|
|
|
|
|
|
There is document metadata, stored in `Document.core_properties`. Edit these
|
|
|
|
values to change metadata.
|
|
|
|
|
|
|
|
== Functions ==
|
|
|
|
|
|
|
|
=== Add headings ===
|
|
|
|
|
|
|
|
`add_heading(text, level)`, where text is the text to add, and level is the
|
|
|
|
size, 0-9, where 0 is the largest.
|
|
|
|
|
|
|
|
=== Add paragraphs ===
|
|
|
|
|
|
|
|
`add_paragraph(text, style)` where text is the text to add, and style is the
|
|
|
|
style to apply. Note that `\n`, `\r`, etc work in the text field.
|
|
|
|
|
|
|
|
=== Page break ===
|
|
|
|
|
|
|
|
`add_page_break()` adds a page break.
|
|
|
|
|
|
|
|
== Style ==
|
|
|
|
|
|
|
|
=== Lists ===
|
|
|
|
|
|
|
|
There are two built in lists, `List Number` and `List Bullet`. `List Number`
|
|
|
|
incremements each time it is added to a paragraph and creats a numbered list.
|
|
|
|
Same applied to `List Bullet`.
|
|
|
|
|
2022-03-16 02:00:01 +00:00
|
|
|
=== Tables ===
|
|
|
|
|
|
|
|
The following are the styles for tables
|
|
|
|
{{{
|
|
|
|
Normal Table
|
|
|
|
Table Grid
|
|
|
|
Light Shading
|
|
|
|
Light Shading Accent 1
|
|
|
|
Light Shading Accent 2
|
|
|
|
Light Shading Accent 3
|
|
|
|
Light Shading Accent 4
|
|
|
|
Light Shading Accent 5
|
|
|
|
Light Shading Accent 6
|
|
|
|
Light List
|
|
|
|
Light List Accent 1
|
|
|
|
Light List Accent 2
|
|
|
|
Light List Accent 3
|
|
|
|
Light List Accent 4
|
|
|
|
Light List Accent 5
|
|
|
|
Light List Accent 6
|
|
|
|
Light Grid
|
|
|
|
Light Grid Accent 1
|
|
|
|
Light Grid Accent 2
|
|
|
|
Light Grid Accent 3
|
|
|
|
Light Grid Accent 4
|
|
|
|
Light Grid Accent 5
|
|
|
|
Light Grid Accent 6
|
|
|
|
Medium Shading 1
|
|
|
|
Medium Shading 1 Accent 1
|
|
|
|
Medium Shading 1 Accent 2
|
|
|
|
Medium Shading 1 Accent 3
|
|
|
|
Medium Shading 1 Accent 4
|
|
|
|
Medium Shading 1 Accent 5
|
|
|
|
Medium Shading 1 Accent 6
|
|
|
|
Medium Shading 2
|
|
|
|
Medium Shading 2 Accent 1
|
|
|
|
Medium Shading 2 Accent 2
|
|
|
|
Medium Shading 2 Accent 3
|
|
|
|
Medium Shading 2 Accent 4
|
|
|
|
Medium Shading 2 Accent 5
|
|
|
|
Medium Shading 2 Accent 6
|
|
|
|
Medium List 1
|
|
|
|
Medium List 1 Accent 1
|
|
|
|
Medium List 1 Accent 2
|
|
|
|
Medium List 1 Accent 3
|
|
|
|
Medium List 1 Accent 4
|
|
|
|
Medium List 1 Accent 5
|
|
|
|
Medium List 1 Accent 6
|
|
|
|
Medium List 2
|
|
|
|
Medium List 2 Accent 1
|
|
|
|
Medium List 2 Accent 2
|
|
|
|
Medium List 2 Accent 3
|
|
|
|
Medium List 2 Accent 4
|
|
|
|
Medium List 2 Accent 5
|
|
|
|
Medium List 2 Accent 6
|
|
|
|
Medium Grid 1
|
|
|
|
Medium Grid 1 Accent 1
|
|
|
|
Medium Grid 1 Accent 2
|
|
|
|
Medium Grid 1 Accent 3
|
|
|
|
Medium Grid 1 Accent 4
|
|
|
|
Medium Grid 1 Accent 5
|
|
|
|
Medium Grid 1 Accent 6
|
|
|
|
Medium Grid 2
|
|
|
|
Medium Grid 2 Accent 1
|
|
|
|
Medium Grid 2 Accent 2
|
|
|
|
Medium Grid 2 Accent 3
|
|
|
|
Medium Grid 2 Accent 4
|
|
|
|
Medium Grid 2 Accent 5
|
|
|
|
Medium Grid 2 Accent 6
|
|
|
|
Medium Grid 3
|
|
|
|
Medium Grid 3 Accent 1
|
|
|
|
Medium Grid 3 Accent 2
|
|
|
|
Medium Grid 3 Accent 3
|
|
|
|
Medium Grid 3 Accent 4
|
|
|
|
Medium Grid 3 Accent 5
|
|
|
|
Medium Grid 3 Accent 6
|
|
|
|
Dark List
|
|
|
|
Dark List Accent 1
|
|
|
|
Dark List Accent 2
|
|
|
|
Dark List Accent 3
|
|
|
|
Dark List Accent 4
|
|
|
|
Dark List Accent 5
|
|
|
|
Dark List Accent 6
|
|
|
|
Colorful Shading
|
|
|
|
Colorful Shading Accent 1
|
|
|
|
Colorful Shading Accent 2
|
|
|
|
Colorful Shading Accent 3
|
|
|
|
Colorful Shading Accent 4
|
|
|
|
Colorful Shading Accent 5
|
|
|
|
Colorful Shading Accent 6
|
|
|
|
Colorful List
|
|
|
|
Colorful List Accent 1
|
|
|
|
Colorful List Accent 2
|
|
|
|
Colorful List Accent 3
|
|
|
|
Colorful List Accent 4
|
|
|
|
Colorful List Accent 5
|
|
|
|
Colorful List Accent 6
|
|
|
|
Colorful Grid
|
|
|
|
Colorful Grid Accent 1
|
|
|
|
Colorful Grid Accent 2
|
|
|
|
Colorful Grid Accent 3
|
|
|
|
Colorful Grid Accent 4
|
|
|
|
Colorful Grid Accent 5
|
|
|
|
Colorful Grid Accent 6
|
|
|
|
}}}
|
2022-03-15 21:30:01 +00:00
|
|
|
|
|
|
|
== Tables ==
|
|
|
|
|
|
|
|
Tables are added via the `add_table(rows, cols, style)` function of the
|
|
|
|
`Document` class.
|
2022-03-16 02:00:01 +00:00
|
|
|
|