Create a PDF from a bunch of files

Create a PDF from a bunch of files

Postby Antonio Linares » Thu Jun 27, 2024 9:35 am

Code: Select all  Expand view
import os
from fpdf import FPDF

class PDF(FPDF):
    def header(self):
        self.set_font('Arial', 'B', 12)
        self.cell(0, 10, 'Repositorio de GitHub', 0, 1, 'C')
       
    def footer(self):
        self.set_y(-15)
        self.set_font('Arial', 'I', 8)
        self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')

    def chapter_title(self, title):
        self.set_font('Arial', 'B', 12)
        self.cell(0, 10, title, 0, 1, 'L')
        self.ln(10)
       
    def chapter_body(self, body):
        self.set_font('Arial', '', 12)
        self.multi_cell(0, 10, body)
        self.ln()

pdf = PDF()
pdf.add_page()

# Recorrer todos los archivos del repositorio
for root, dirs, files in os.walk('.'):
    for file in files:
        if file.endswith('.prg') or file.endswith('.ch') or file.endswith('.c'):  # filtra según los tipos de archivos que quieras incluir
            file_path = os.path.join(root, file)
            with open(file_path, 'r', encoding='utf-8') as f:
                content = f.read()
                pdf.chapter_title(file_path)
                pdf.chapter_body(content)

# Guardar el PDF
pdf.output('repositorio.pdf')
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42098
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Create a PDF from a bunch of files

Postby Antonio Linares » Fri Jun 28, 2024 8:25 am

FWH version thanks to Mr. Rao:

Code: Select all  Expand view
function PrgsPdf()

   local cPath    := "c:\fwh\samples\"
   local cPdf     := "
prgs.pdf"
   local aFiles   := {}
   local oPrn, oFont, nPg, cFile, cText, nRow

   AEval( DIRECTORY( cPath + "
*.prg" ), { |aFile| AAdd( aFiles, cPath + aFile[ 1 ] ) }, 1, 10 )

   PRINT oPrn PREVIEW FILE cPdf
   oPrn:SetPage( 9 ) // A4

   DEFINE FONT oFont NAME "
ARIAL" SIZE 0,-10 OF oPrn

   for each cFile in aFiles
      cText    := MEMOREAD( cFile )
      nPg      := 0
      do while !Empty( cText )
         PAGE
         @ 0.5, 1.0 PRINT TO oPrn TEXT cFile + "
: Page : " + cValToChar( ++nPg ) SIZE 7.0, 1 INCHES FONT oFont ;
            LASTROW nRow
         oPrn:Line( nRow, 1, nRow, 8, , "
INCHES" )
         @  1.0, 1.0 PRINT TO oPrn TEXT @cText SIZE 7, 9.5 INCHES FONT oFont
         oPrn:Line( 10.7, 1, 10.7, 8, , "
INCHES" )
         @ 11.0, 1.0 PRINT TO oPrn TEXT "
Page : " + cValToChar( oPrn:nPage ) + "/" + oPrn:cLastPage ;
            SIZE 7,1 INCHES FONT oFont ALIGN "
TR"
         ENDPAGE
      enddo
   next
   ENDPRINT
   RELEASE FONT oFont

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42098
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to Utilities / Utilidades

Who is online

Users browsing this forum: No registered users and 7 guests