Skip to content

Instantly share code, notes, and snippets.

@jesselau76
Created November 10, 2023 03:20
Show Gist options
  • Save jesselau76/c0937596a4f3a04ef06193f432f9fcf2 to your computer and use it in GitHub Desktop.
Save jesselau76/c0937596a4f3a04ef06193f432f9fcf2 to your computer and use it in GitHub Desktop.
split-text-to-pdfs
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
def split_text_into_chunks(text, num_chunks):
"""Split the text into specified number of chunks."""
lines_per_chunk = len(text) // num_chunks
return [text[i * lines_per_chunk:(i + 1) * lines_per_chunk] for i in range(num_chunks - 1)] + [text[(num_chunks - 1) * lines_per_chunk:]]
def create_pdf(text_lines, filename, font_path, font_name='SimFang', font_size=10):
"""Create a PDF from a list of text lines, with multiple pages if necessary."""
pdfmetrics.registerFont(TTFont(font_name, font_path))
c = canvas.Canvas(filename, pagesize=A4)
textobject = c.beginText(inch * 0.5, A4[1] - inch)
textobject.setFont(font_name, font_size)
for line in text_lines:
# Check if the text goes beyond the page limit
if textobject.getY() < inch:
c.drawText(textobject)
c.showPage()
textobject = c.beginText(inch * 0.5, A4[1] - inch)
textobject.setFont(font_name, font_size)
textobject.textLine(line.strip())
# Don't forget to draw the text for the last page
c.drawText(textobject)
c.showPage()
c.save()
def main():
input_file = {textfile} # Replace with your text file path
font_file = 'simfang.ttf' # Replace with your font file path
num_parts = {filenumbers}
# Read the content of the file
with open(input_file, 'r', encoding='utf-8') as file:
lines = file.readlines()
# Split the text into 5 chunks
text_chunks = split_text_into_chunks(lines, num_parts)
# Create and save PDF files
for i, chunk in enumerate(text_chunks):
pdf_filename = f'sunzi_part{i+1}.pdf'
print(chunk)
create_pdf(chunk, pdf_filename, font_file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment