-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_songs_alphabetically.py
More file actions
56 lines (46 loc) · 1.87 KB
/
sort_songs_alphabetically.py
File metadata and controls
56 lines (46 loc) · 1.87 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
"""Script to sort individual LaTeX song files alphabetically by title, renumber them,
and combine into a single LaTeX file with two-column layout."""
import os
import re
# Folder where all individual .tex songs are stored
SONGS_FOLDER = "songs"
# Output file that contains all songs in alphabetical order
OUTPUT_FILE = "songs_sorted.tex"
# Collect all .tex song files
song_files = [f for f in os.listdir(SONGS_FOLDER) if f.endswith(".tex")]
songs = []
for filename in song_files:
filepath = os.path.join(SONGS_FOLDER, filename)
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
# Extract the title from \songtitle{...}
match = re.search(r"\\songtitle\{(.+?)\}", content)
if match:
title = match.group(1).strip()
songs.append((title, filename, content))
else:
print(f"⚠️ Warning: No title found in {filename}")
# Sort songs alphabetically by title
songs_sorted = sorted(songs, key=lambda x: x[0].lower())
# Build LaTeX body with renumbered titles
renumbered_blocks = []
for i, (title, filename, content) in enumerate(songs_sorted, start=1):
# Replace the title with "N. Title"
new_content = re.sub(
r"\\songtitle\{.+?\}",
f"\\\\songtitle{{{i}. {title}}}",
content,
count=1
)
# Clean up extra whitespaces
#new_content = '\n'.join(line for line in new_content.split('\n') if line.strip())
# Add commands to prevent page/column breaks within songs
renumbered_blocks.append(new_content)
# Write to output file
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write("% Auto-generated file, do not edit manually\n\n")
f.write("\\begin{multicols}{2}\n")
f.write("\n\n".join(renumbered_blocks))
f.write("\n\\end{multicols}\n")
print(f"✅ Alphabetical songbook with numbering written to {OUTPUT_FILE}")
print(f"✅ Total songs: {len(songs_sorted)}")