Skip to content

Instantly share code, notes, and snippets.

@schnell18
Last active May 17, 2025 15:56
Show Gist options
  • Save schnell18/17731bfcd4fa2142d6fccbccf51975b3 to your computer and use it in GitHub Desktop.
Save schnell18/17731bfcd4fa2142d6fccbccf51975b3 to your computer and use it in GitHub Desktop.
This script generates a three-column long table that runs over multiple pages in LaTeX given a two-column time and description text file.
#!/usr/bin/env python
import re
import sys
HEADER = r"""
\documentclass{article}
\usepackage{booktabs,makecell,multirow,threeparttable,diagbox}
\usepackage{longtable}
\usepackage{fancyvrb}
\usepackage{caption}
\usepackage{pdflscape}
\usepackage[a4paper, margin=1.5cm]{geometry}
\usepackage{fancyhdr}
% Set up fancy page style
\pagestyle{fancy}
\fancyhf{} % Clear all headers/footers
\renewcommand{\headrulewidth}{0pt} % Remove header line
\usepackage[
colorlinks=true,
linkcolor=blue,
bookmarksnumbered=true,
CJKbookmarks=true,
bookmarksopen=true]{hyperref}
\begin{document}
\newgeometry{left=1cm, right=1cm, top=2cm, bottom=2cm}
\begin{landscape}
%\fancyfoot[C]{\thepage} % Page number at bottom center
\begin{longtable}{p{16em}rp{16em}rp{16em}r}
\caption{AWS SAA Training Video Bookmarks} \\
\label{tab:tspsaa} \\
\toprule
{\textbf{Topic}} & {\textbf{Time}} &
{\textbf{Topic}} & {\textbf{Time}} &
{\textbf{Topic}} & {\textbf{Time}} \\
\midrule
\endfirsthead
\toprule
{\textbf{Topic}} & {\textbf{Time}} &
{\textbf{Topic}} & {\textbf{Time}} &
{\textbf{Topic}} & {\textbf{Time}} \\
\midrule
\endhead
\bottomrule
\multicolumn{6}{r}{{\tablename\ \thetable{} -- \emph{Continue}}} \\
\endfoot
\bottomrule
\endlastfoot
"""
FOOTER = r"""
\end{longtable}
\end{landscape}
\restoregeometry % Reset to the original margins after landscape
\end{document}
"""
if __name__ == "__main__":
batch_size = 3
base_url = "https://www.youtube.com/watch?v=c3Cn4xYfxJY"
topics = []
for seq, line in enumerate(sys.stdin):
line = line.rstrip()
time, title = line.split(" ", 1)
m = re.match(r"(\d+):(\d+):(\d+)", time)
if m:
hour = int(m.group(1))
min = int(m.group(2))
sec = int(m.group(3))
t_secs = hour * 3600 + min * 60 + sec
link = f"{base_url}\\&t={t_secs}"
topics.append((title, time, link))
else:
topics.append((time, title, None))
# print topics in table
# print header
print(HEADER)
# print rows
rows = len(topics) // batch_size
remainder = len(topics) % batch_size
for i in range(rows):
for j in range(batch_size):
tup = topics[i * batch_size + j]
title, time, link = tup
print(f"{title} & \\href{{{link}}}{{{time}}} ", end="")
if j != batch_size - 1:
print(" & ", end="")
print(r"\\")
if remainder != 0:
for i in range(remainder):
tup = topics[rows * batch_size + i]
title, time, link = tup
print(f"{title} & \\href{{{link}}}{{{time}}} ", end="")
print(" & ", end="")
for i in range(batch_size - remainder):
print(" & ", end="")
if i != batch_size - remainder - 1:
print(" & ", end="")
print(r"\\")
print(FOOTER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment