This repository was archived by the owner on Jan 7, 2024. It is now read-only.
forked from holli-holzer/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblkcntnr.py
More file actions
70 lines (61 loc) · 2.21 KB
/
Copy pathblkcntnr.py
File metadata and controls
70 lines (61 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# encoding: utf-8
"""
Block item container, used by body, cell, header, etc. Block level items are
things like paragraph and table, although there are a few other specialized
ones like structured document tags.
"""
from __future__ import absolute_import, print_function
from .shared import Parented
from .text import Paragraph
class BlockItemContainer(Parented):
"""
Base class for proxy objects that can contain block items, such as _Body,
_Cell, header, footer, footnote, endnote, comment, and text box objects.
Provides the shared functionality to add a block item like a paragraph or
table.
"""
def __init__(self, element, parent):
super(BlockItemContainer, self).__init__(parent)
self._element = element
def add_paragraph(self, text='', style=None):
"""
Return a paragraph newly added to the end of the content in this
container, having *text* in a single run if present, and having
paragraph style *style*. If *style* is |None|, no paragraph style is
applied, which has the same effect as applying the 'Normal' style.
"""
p = self._element.add_p()
paragraph = Paragraph(p, self)
if text:
paragraph.add_run(text)
if style is not None:
paragraph.style = style
return paragraph
def add_table(self, rows, cols):
"""
Return a newly added table having *rows* rows and *cols* cols,
appended to the content in this container.
"""
from .table import Table
tbl = self._element.add_tbl()
table = Table(tbl, self)
for i in range(cols):
table.add_column()
for i in range(rows):
table.add_row()
return table
@property
def paragraphs(self):
"""
A list containing the paragraphs in this container, in document
order. Read-only.
"""
return [Paragraph(p, self) for p in self._element.p_lst]
@property
def tables(self):
"""
A list containing the tables in this container, in document order.
Read-only.
"""
from .table import Table
return [Table(tbl, self) for tbl in self._element.tbl_lst]