-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyflashcards.py
75 lines (58 loc) · 2.19 KB
/
pyflashcards.py
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
71
72
73
74
75
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER
from reportlab.platypus import Paragraph
from math import ceil
import re
def create_cards(input_file, output_file):
# Read sentences from file
with open(input_file, 'r', encoding='utf-8') as f:
sentences = [line.strip() for line in f if line.strip()]
# Calculate number of pages needed
cards_per_page = 12
num_pages = ceil(len(sentences) / cards_per_page)
# Set up the document
c = canvas.Canvas(output_file, pagesize=A4)
width, height = A4
# Card dimensions
card_width = width / 2
card_height = height / 6
# Margins
margin = 15 * mm
styles = getSampleStyleSheet()
style = ParagraphStyle('Custom',
parent=styles['Normal'],
alignment=TA_CENTER,
fontSize=12,
leading=14)
def draw_wrapped_text(c, text, x, y, width, height):
# Convert BBCode to HTML
text = re.sub(r'\[b\](.*?)\[/b\]', r'<b>\1</b>', text)
text = re.sub(r'\[i\](.*?)\[/i\]', r'<i>\1</i>', text)
p = Paragraph(text, style)
w, h = p.wrap(width - 2*margin, height - 2*margin)
# Calculate vertical centering
y_centered = y + (height - h) / 2
# Draw the paragraph
p.drawOn(c, x + margin, y_centered)
for page in range(num_pages):
for i in range(cards_per_page):
if page * cards_per_page + i >= len(sentences):
break
# Calculate card position
row = i // 2
col = i % 2
x = col * card_width
y = height - (row + 1) * card_height
# Draw card outline
c.rect(x, y, card_width, card_height)
# Add sentence
sentence = sentences[page * cards_per_page + i]
draw_wrapped_text(c, sentence, x, y, card_width, card_height)
c.showPage()
c.save()
# Usage
create_cards('sentence_list.txt', 'output_cards.pdf')