From de61cfa258d6a28a3163cb63a0279048e7852243 Mon Sep 17 00:00:00 2001 From: itec78 Date: Thu, 19 Aug 2021 13:40:39 +0200 Subject: [PATCH] index --- oloturia2pdf.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/oloturia2pdf.py b/oloturia2pdf.py index 3b946cc..038c353 100755 --- a/oloturia2pdf.py +++ b/oloturia2pdf.py @@ -57,6 +57,52 @@ def copertina(text): return(page) +def indice(text): + # PDF GENERATION LIBRARIES + # import the report lab PDF generation tools + from reportlab.lib.pagesizes import letter + from reportlab.lib.styles import ParagraphStyle + from reportlab.lib.units import inch + from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak + from reportlab.pdfbase import pdfmetrics + from reportlab.pdfbase.ttfonts import TTFont + from reportlab.lib.pagesizes import A5 + import io + from PyPDF2 import PdfFileWriter, PdfFileReader + + parts = [] + + pdfmetrics.registerFont(TTFont("Roboto", "template/roboto-regular-webfont.ttf")) + style = ParagraphStyle( + name='Normal', + fontName='Roboto', + fontSize=12, + leading = 14, + leftIndent=32, + firstLineIndent=-32, + spaceBefore=5 + ) + + for ro in text.splitlines(): + ro = ro.replace(' ',' ') + ro = ro.replace('\t','    ') + parts.append(Paragraph(ro, style = style)) + + + + packet = io.BytesIO() + doc = SimpleDocTemplate(packet, + pagesize=A5, + rightMargin=20, + leftMargin=20, + topMargin=40, + bottomMargin=30) + doc.build(parts) + + pdfReader = PdfFileReader(packet) + return([pdfReader.getPage(0),pdfReader.getPage(1)]) + + def main(): # Scarica tutti i post da Mastodon @@ -110,6 +156,8 @@ def main(): for vgo in all_vgos: vgo_num = html2text.html2text(vgo['content']).split(' ')[0] vgo_name = os.linesep.join([s for s in html2text.html2text(vgo['content']).splitlines() if s]).splitlines()[-1] + if len(vgo_name) < 10: + vgo_name = [s for s in html2text.html2text(vgo['content']).split("\n\n") if s][-1].replace("\n"," ") #print(vgo_num +' - '+ vgo_name) #print(str(vgo['id']) +' '+ vgo['uri']) vgo_dict[vgo_num] = vgo_name @@ -211,12 +259,14 @@ def main(): # aggiungere copertina pdfWriter.addPage(copertina(str(pagstart).zfill(3) + " - " + str(pagend).zfill(3))) - + + indtext = "" for vgo_num in [str(x).zfill(3) for x in range(pagstart, pagend + 1)]: pdf_name = os.path.join('pdf', vgo_num + '.pdf') try: #print(vgo_num + " - " + vgo_dict[vgo_num]) + indtext = indtext + vgo_num + "\t" + vgo_dict[vgo_num] + "\n" pdfFileObj = open(pdf_name, 'rb') pdfReader = PyPDF2.PdfFileReader(pdfFileObj) pageObj = pdfReader.getPage(0) @@ -225,6 +275,9 @@ def main(): pass # aggiungere indice ed eventualmente pagina finale + for indpag in indice(indtext): + pdfWriter.addPage(indpag) + book_name = os.path.join('books', 'book' + str(book_num).zfill(2) + '.pdf') with open(book_name, 'wb') as pdfOutput: