Merge pdf files: R and Python

 Merge PDF

 

R

# Install the necessary packages if not already installed
# install.packages("pdftools")
# install.packages("qpdf")

# Load the packages
library(pdftools)
library(qpdf)

path <- c("C:/path_to_file_folder/")

# Define the file paths of the PDFs to be merged

pdf_files <- c(
paste0(path, "C_file", ".pdf"),
paste0(path, "B
_file", ".pdf"),
paste0(path, "A
_file", ".pdf")
)

# Merge the PDF files into a single PDF
pdf_combine(
input = pdf_files
, output = paste0(path, "merge_file", ".pdf")
)


Python

from PyPDF2 import PdfMerger, PdfReader
import os


pdfiles = []
for filename in os.listdir('.'):
        if filename.endswith('.pdf'):
                if filename != 'merged.pdf':
                        pdfiles.append(filename)
                       
pdfiles.sort(key = str.lower)

pdfMerge = PdfMerger()
for filename in pdfiles:
        pdfFile = open(filename, 'rb')
        pdfReader = PdfReader(pdfFile)
        pdfMerge.append(pdfReader)
pdfFile.close()
pdfMerge.write('merged.pdf')

"""

listaPdfs = os.listdir('C:/path_to_pdf/')
listaPdfs

['iza-Writing-Tips-for-Economics-Research.pdf', 'dp15057.pdf']

# merger = PdfFileMerger()
merger = PdfFileMerger()

for file in listaPdfs:
    merger.append(PdfFileReader('../Pdfs/'+file))#, import_bookmarks=False)

    merger.write('../Output/PdfFinal.pdf')

    """


 docxtpl

pip install docxtpl


https://docxtpl.readthedocs.io/en/latest/

-code-------

from docxtpl import DocxTemplate

doc = DocxTemplate("my_word_template.docx")
context = { 'variable_in_template_name' : "World company" }
doc.render(context)
doc.save("generated_doc.docx")

/-----------------

Word:

{{variable_in_template_name}}




Plantilla de Word



Excel to PDF


pip install pywin32


Entradas populares

SQL