TG-46 test fusion pdf

This commit is contained in:
DonRenando 2017-01-24 11:14:19 +01:00
parent 1b6d51516d
commit 46dcdb82e7
5 changed files with 60 additions and 25 deletions

View File

@ -2,9 +2,24 @@ import os
from PyPDF2 import PdfFileReader, PdfFileMerger
def fusion_fichiers(chemin_des_pdf, chemin_merge_pdf, nom_merge_pdf):
pdf_files = [f for f in os.listdir(chemin_des_pdf) if f.endswith("pdf")]
def fusion_fichiers(chemin_merge_pdf, nom_merge_pdf, liste_de_pdf):
"""
Permet de fusionner un ensemble de pdf
:param chemin_merge_pdf: chemin ou l'on souhaite fusioner l'ensemble des pdf
:param nom_merge_pdf: nom que l'on souhaite donner au fichier pdf final
:param liste_de_pdf: liste de pdf avec leur chemin inclu
:return:
"""
merger = PdfFileMerger()
for filename in pdf_files:
merger.append(PdfFileReader(os.path.join(chemin_des_pdf, filename), "rb"))
for filename in liste_de_pdf:
merger.append(PdfFileReader(os.path.join(filename), "rb"))
merger.write(os.path.join(chemin_merge_pdf, nom_merge_pdf))
def get_pdf_from_directory(chemin_des_pdf):
"""
Permet de récuperer l'ensemble des pdf d'un chemin
:param chemin_des_pdf:
:return:
"""
return [f for f in os.listdir(chemin_des_pdf) if f.endswith("pdf")]

View File

@ -0,0 +1,21 @@
import os
import unittest
from pathlib import Path
from builtins import print
from app.tools.FusionPdf import fusion_fichiers, get_pdf_from_directory
class TestFusionTestCase(unittest.TestCase):
def setUp(self):
self.datadir = os.path.join(os.path.dirname(__file__))
self.list_pdf = [self.datadir + "/page1.pdf", self.datadir + "/page2.pdf"]
def test_fusion(self):
fusion_fichiers(self.datadir, "testFusion.pdf", self.list_pdf)
print(self.datadir)
self.assertTrue(Path(self.datadir + "/testFusion.pdf").is_file(), "Pdf fusionne inexistant")
self.assertTrue(len(get_pdf_from_directory(self.datadir)) > 0, "pdf non trouve")
os.remove(self.datadir + "/testFusion.pdf")

Binary file not shown.

Binary file not shown.

View File

@ -8,8 +8,7 @@ from pdfjinja import PdfJinja
from app.tools.InsertTemplate import remplir_template
class insertTemplateTestCase(unittest.TestCase):
class InsertTemplateTestCase(unittest.TestCase):
datadir = os.path.join(os.path.dirname(__file__))
def setUp(self):