Skip to content

Commit

Permalink
Merge pull request JelteF#183 from JarrahGos/longtables
Browse files Browse the repository at this point in the history
Add foot and last foot commands to longtables
  • Loading branch information
JelteF authored Jul 4, 2017
2 parents 73d9a9e + 60f760c commit 64d0e7f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ This version might not be stable, but to install it use::

pip install git+https://github.com/JelteF/PyLaTeX.git

Added
~~~~~
- Longtables now have end_foot() and end_last_foot() functions.

1.2.1_ - `docs <../v1.2.1/>`__ - 2017-05-19
-------------------------------------------

Expand Down
44 changes: 44 additions & 0 deletions examples/longtable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python
"""
This example shows the functionality of the longtable element.
It creates a sample multi-page spanning table
.. :copyright: (c) 2017 by Jarrah Gosbell
:license: MIT, see License for more details.
"""

# begin-doc-include
from pylatex import Document, LongTable, MultiColumn


def genenerate_longtabu():
geometry_options = {
"margin": "2.54cm",
"includeheadfoot": True
}
doc = Document(page_numbers=True, geometry_options=geometry_options)

# Generate data table
with doc.create(LongTable("l l l")) as data_table:
data_table.add_hline()
data_table.add_row(["header 1", "header 2", "header 3"])
data_table.add_hline()
data_table.end_table_header()
data_table.add_hline()
data_table.add_row((MultiColumn(3, align='r',
data='Containued on Next Page'),))
data_table.add_hline()
data_table.end_table_footer()
data_table.add_hline()
data_table.add_row((MultiColumn(3, align='r',
data='Not Containued on Next Page'),))
data_table.add_hline()
data_table.end_table_last_footer()
row = ["Content1", "9", "Longer String"]
for i in range(150):
data_table.add_row(row)

doc.generate_pdf("longtable", clean_tex=False)

genenerate_longtabu()
26 changes: 25 additions & 1 deletion pylatex/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ class LongTable(Tabular):
packages = [Package('longtable')]

header = False
foot = False
lastFoot = False

def end_table_header(self):
r"""End the table header which will appear on every page."""
Expand All @@ -464,7 +466,29 @@ def end_table_header(self):

self.header = True

self.append(NoEscape(r'\endhead'))
self.append(Command(r'endhead'))

def end_table_footer(self):
r"""End the table foot which will appear on every page."""

if self.foot:
msg = "Table already has a foot"
raise TableError(msg)

self.foot = True

self.append(Command('endfoot'))

def end_table_last_footer(self):
r"""End the table foot which will appear on the last page."""

if self.lastFoot:
msg = "Table already has a last foot"
raise TableError(msg)

self.lastFoot = True

self.append(Command('endlastfoot'))


class LongTabu(LongTable, Tabu):
Expand Down

0 comments on commit 64d0e7f

Please sign in to comment.