<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>python programming on File Format Blog</title>
    <link>https://blog.fileformat.com/fr/tag/python-programming/</link>
    <description>Recent content in python programming on File Format Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>fr</language>
    <lastBuildDate>Wed, 29 Jan 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.fileformat.com/fr/tag/python-programming/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Travailler avec des fichiers PDF en Python</title>
      <link>https://blog.fileformat.com/fr/programming/working-with-pdf-files-in-python/</link>
      <pubDate>Wed, 29 Jan 2025 00:00:00 +0000</pubDate>
      
      <guid>https://blog.fileformat.com/fr/programming/working-with-pdf-files-in-python/</guid>
      <description>Apprenez à extraire du texte d&amp;#39;un PDF en Python, à faire pivoter les pages d&amp;#39;un PDF, à fusionner plusieurs PDF, à diviser des PDF, et à ajouter des filigranes à vos PDF en utilisant des bibliothèques Python et des exemples de code simples.</description>
      <content:encoded><![CDATA[<p><strong>Dernière mise à jour</strong>: 29 janvier 2025</p>
<figure class="align-center ">
    <img loading="lazy" src="images/working-with-pdf-files-in-python.png#center"
         alt="Titre - Travailler avec des fichiers PDF en Python"/> 
</figure>

<p>Dans cet article, nous vous guiderons sur <strong>comment travailler avec des fichiers PDF en utilisant Python</strong>. Pour cela, nous utiliserons la bibliothèque <a href="https://pypi.org/project/pypdf/"><strong>pypdf</strong></a>.</p>
<p>En utilisant la bibliothèque <strong>pypdf</strong>, nous vous montrerons comment effectuer les opérations suivantes en Python :</p>
<ul>
<li>Extraction de texte de PDF</li>
<li>Rotation des pages de PDF</li>
<li>Fusion de plusieurs PDF</li>
<li>Division de PDF en fichiers séparés</li>
<li>Ajout de filigranes aux pages PDF</li>
</ul>
<p><em><strong>Remarque</strong>: Cet article couvre de nombreux détails précieux, n&rsquo;hésitez donc pas à passer aux sections qui vous intéressent le plus ! Le contenu est organisé pour une navigation facile, afin que vous puissiez rapidement vous concentrer sur ce qui vous est le plus pertinent.</em></p>
<figure class="align-center ">
    <img loading="lazy" src="images/pdf-manipulation-with-pypdf.webp#center"
         alt="Illustration - Travailler avec des fichiers PDF en Python"/> 
</figure>

<h2 id="codes-dexemple">Codes d&rsquo;exemple</h2>
<p>Vous pouvez télécharger tout le code d&rsquo;exemple utilisé dans cet article depuis le lien suivant. Il comprend le code, les fichiers d&rsquo;entrée et les fichiers de sortie.</p>
<ul>
<li><a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python">Exemples de code et fichiers d&rsquo;entrée pour travailler avec des fichiers PDF en Python</a></li>
</ul>
<h2 id="installer-pypdf">Installer pypdf</h2>
<p>Pour installer pypdf, exécutez simplement la commande suivante dans votre terminal ou invite de commande :</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>pip install pypdf
</span></span></code></pre></div><p><strong>Remarque:</strong> La commande ci-dessus est sensible à la casse.</p>
<h2 id="1-extraction-de-texte-dun-fichier-pdf-en-utilisant-python">1. Extraction de texte d&rsquo;un fichier PDF en utilisant Python</h2>
<script type="application/javascript" src="https://gist.github.com/fileformat-blog-gists/e2b43a49dbad9e89745f8f9777817acb.js?file=extract-text-from-pdf-using-pypdf-in-python.py"></script>

<h3 id="explication-du-code"><strong>Explication du Code</strong></h3>
<p><strong>1. Création d&rsquo;un objet lecteur de PDF</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>reader <span style="color:#f92672">=</span> PdfReader(pdf_file)
</span></span></code></pre></div><ul>
<li><code>PdfReader(pdf_file)</code> charge le fichier PDF dans un <strong>objet lecteur</strong>.</li>
<li>Cet objet permet d&rsquo;accéder aux pages et à leur contenu.</li>
</ul>
<p><strong>2. Boucle à travers les pages</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">for</span> page_number, page <span style="color:#f92672">in</span> enumerate(reader<span style="color:#f92672">.</span>pages, start<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>):
</span></span></code></pre></div><ul>
<li><code>reader.pages</code> renvoie une liste de pages dans le PDF.</li>
<li><code>enumerate(..., start=1)</code> assigne un <strong>numéro de page à partir de 1</strong>.</li>
</ul>
<p><strong>3. Impression du texte extrait</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>    print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Page </span><span style="color:#e6db74">{</span>page_number<span style="color:#e6db74">}</span><span style="color:#e6db74">:&#34;</span>)
</span></span><span style="display:flex;"><span>    print(page<span style="color:#f92672">.</span>extract_text())
</span></span><span style="display:flex;"><span>    print(<span style="color:#e6db74">&#34;-&#34;</span> <span style="color:#f92672">*</span> <span style="color:#ae81ff">50</span>)  <span style="color:#75715e"># Séparateur pour la lisibilité</span>
</span></span></code></pre></div><ul>
<li><code>page.extract_text()</code> extrait le contenu texte de la page actuelle.</li>
<li>Le script imprime le texte extrait avec le <strong>numéro de page</strong>.</li>
<li><code>&quot;-&quot; * 50</code> imprime une ligne de séparation (<code>--------------------------------------------------</code>) pour une meilleure lisibilité.</li>
</ul>
<h3 id="fichier-pdf-dentrée-utilisé-dans-le-code">Fichier PDF d&rsquo;entrée utilisé dans le code</h3>
<ul>
<li><strong>Fichier d&rsquo;entrée :</strong> <a href="https://github.com/fileformat-blog-gists/code/blob/main/working-with-pdf-files-in-python/pdf-to-extract-text/">Lien de téléchargement</a></li>
</ul>
<h3 id="sortie-du-code">Sortie du Code</h3>
<script type="application/javascript" src="https://gist.github.com/fileformat-blog-gists/ab6976aa3a0fc2999093f5f9320a9e20.js?file=Output%20-%20extract-text-from-pdf-using-pypdf-in-python.txt"></script>

<h2 id="2-rotation-des-pages-pdf-en-utilisant-python">2. Rotation des pages PDF en utilisant Python</h2>
<script type="application/javascript" src="https://gist.github.com/fileformat-blog-gists/760d480cfede4178296c353d60662e1a.js?file=rotate-pdf-page-using-pypdf-in-python.py"></script>

<h3 id="explication-du-code-1">Explication du Code</h3>
<p>Le code permet essentiellement de faire pivoter la <strong>première page</strong> de <strong>90° dans le sens horaire</strong> et de sauvegarder le PDF modifié sans affecter les autres pages.</p>
<p><strong>1. Importer les classes requises</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> pypdf <span style="color:#f92672">import</span> PdfReader, PdfWriter
</span></span></code></pre></div><ul>
<li><code>PdfReader</code>: Lit le PDF d&rsquo;entrée.</li>
<li><code>PdfWriter</code>: Crée un nouveau PDF avec des modifications.</li>
</ul>
<p><strong>2. Définir les chemins des fichiers d&rsquo;entrée et de sortie</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>input_pdf <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;pdf-to-rotate/input.pdf&#34;</span>
</span></span><span style="display:flex;"><span>output_pdf <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;pdf-to-rotate/rotated_output.pdf&#34;</span>
</span></span></code></pre></div><ul>
<li>Le script lit à partir de <code>input.pdf</code> et sauvegarde le fichier modifié sous le nom de <code>rotated_output.pdf</code>.</li>
</ul>
<p><strong>3. Lire le PDF et créer un objet Writer</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>reader <span style="color:#f92672">=</span> PdfReader(input_pdf)
</span></span><span style="display:flex;"><span>writer <span style="color:#f92672">=</span> PdfWriter()
</span></span></code></pre></div><ul>
<li><code>reader</code> charge le PDF existant.</li>
<li><code>writer</code> est utilisé pour stocker les pages modifiées.</li>
</ul>
<p><strong>4. Faire pivoter la première page de 90 degrés</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>page <span style="color:#f92672">=</span> reader<span style="color:#f92672">.</span>pages[<span style="color:#ae81ff">0</span>]
</span></span><span style="display:flex;"><span>page<span style="color:#f92672">.</span>rotate(<span style="color:#ae81ff">90</span>)  <span style="color:#75715e"># Pivoter de 90 degrés dans le sens horaire</span>
</span></span><span style="display:flex;"><span>writer<span style="color:#f92672">.</span>add_page(page)
</span></span></code></pre></div><ul>
<li>Extrait <strong>la page 1</strong>, la fait pivoter de <strong>90 degrés</strong>, et l&rsquo;ajoute au nouveau PDF.</li>
</ul>
<p><strong>5. Ajouter les pages restantes sans modifications</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>, len(reader<span style="color:#f92672">.</span>pages)):
</span></span><span style="display:flex;"><span>    writer<span style="color:#f92672">.</span>add_page(reader<span style="color:#f92672">.</span>pages[i])
</span></span></code></pre></div><ul>
<li>Parcourt les pages restantes et les ajoute telles quelles.</li>
</ul>
<p><strong>6. Sauvegarder le nouveau PDF</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">with</span> open(output_pdf, <span style="color:#e6db74">&#34;wb&#34;</span>) <span style="color:#66d9ef">as</span> file:
</span></span><span style="display:flex;"><span>    writer<span style="color:#f92672">.</span>write(file)
</span></span></code></pre></div><ul>
<li>Ouvre <code>rotated_output.pdf</code> en mode écriture-binaire et enregistre le nouveau PDF.</li>
</ul>
<p><strong>7. Imprimer la confirmation</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Rotated page saved to </span><span style="color:#e6db74">{</span>output_pdf<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
</span></span></code></pre></div><ul>
<li>Affiche un message de succès.</li>
</ul>
<h3 id="fichier-pdf-dentrée-utilisé-dans-le-code-et-sa-sortie-pivotée">Fichier PDF d&rsquo;entrée utilisé dans le code et sa sortie pivotée</h3>
<ul>
<li><strong>Fichier PDF d&rsquo;entrée :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-rotate/">Lien de téléchargement</a></li>
<li><strong>Fichier PDF Pivoté en Sortie :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-rotate/rotated_output.pdf">Lien de téléchargement</a></li>
</ul>
<p><strong>Capture d&rsquo;écran</strong>
<img loading="lazy" src="https://raw.githubusercontent.com/fileformat-blog-gists/content/main/working-with-pdf-files-in-python/rotated-pdf.png" alt="Capture d&amp;rsquo;écran de la page pivotée dans le PDF en utilisant Python"  />
</p>
<h2 id="3-fusionner-des-fichiers-pdf-en-utilisant-python">3. Fusionner des fichiers PDF en utilisant Python</h2>
<p>Ce script Python démontre comment <strong>fusionner plusieurs fichiers PDF</strong> d&rsquo;un répertoire en un seul PDF en utilisant la bibliothèque <strong>PyPDF</strong>.</p>
<script type="application/javascript" src="https://gist.github.com/fileformat-blog-gists/a1a571783e0f5e699678d1094bf1afa5.js?file=merge_pdf_files_using_pypdf_in_python.py"></script>

<h3 id="explication-du-code-2">Explication du Code</h3>
<ul>
<li>Ce script fusionne automatiquement tous les fichiers PDF trouvés dans le répertoire spécifié (<code>pdfs-to-merge</code>) en un seul fichier de sortie (<code>merged_output.pdf</code>).</li>
<li>Il s&rsquo;assure que le répertoire de sortie existe et ajoute les pages de chaque PDF dans l&rsquo;ordre où elles sont listées.</li>
<li>Il génère le fichier final fusionné dans le sous-répertoire <code>output-dir</code>.</li>
</ul>
<p><strong>Détail du Code</strong></p>
<p><strong>1. Importer les bibliothèques</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">import</span> os
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> pypdf <span style="color:#f92672">import</span> PdfReader, PdfWriter
</span></span></code></pre></div><ul>
<li><code>os</code>: Utilisé pour interagir avec le système de fichiers, tel que la lecture de répertoires et la gestion des chemins de fichiers.</li>
<li><code>PdfReader</code>: Lit le contenu d&rsquo;un fichier PDF.</li>
<li><code>PdfWriter</code>: Crée et écrit un nouveau fichier PDF.</li>
</ul>
<p><strong>2. Définir le répertoire et fichier de sortie</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>directory <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;pdfs-to-merge&#34;</span>
</span></span><span style="display:flex;"><span>output_file <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;output-dir/merged_output.pdf&#34;</span>
</span></span></code></pre></div><ul>
<li><code>directory</code>: Spécifie le dossier où sont stockés les fichiers PDF.</li>
<li><code>output_file</code>: Définit le chemin et le nom de sortie du PDF fusionné.</li>
</ul>
<p><strong>3. Créer le répertoire de sortie s&rsquo;il n&rsquo;existe pas</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>os<span style="color:#f92672">.</span>makedirs(os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>join(directory, <span style="color:#e6db74">&#34;output-dir&#34;</span>), exist_ok<span style="color:#f92672">=</span><span style="color:#66d9ef">True</span>)
</span></span></code></pre></div><ul>
<li>Cela s&rsquo;assure que le <strong>répertoire de sortie</strong> existe, et s&rsquo;il n&rsquo;existe pas, il le crée.</li>
</ul>
<p><strong>4. Créer un objet PdfWriter</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>writer <span style="color:#f92672">=</span> PdfWriter()
</span></span></code></pre></div><ul>
<li><code>writer</code> est utilisé pour collecter et combiner toutes les pages des PDF.</li>
</ul>
<p><strong>5. Itérer sur tous les fichiers PDF du répertoire</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">for</span> file_name <span style="color:#f92672">in</span> sorted(os<span style="color:#f92672">.</span>listdir(directory)):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> file_name<span style="color:#f92672">.</span>endswith(<span style="color:#e6db74">&#34;.pdf&#34;</span>):
</span></span><span style="display:flex;"><span>        file_path <span style="color:#f92672">=</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>join(directory, file_name)
</span></span><span style="display:flex;"><span>        print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Adding: </span><span style="color:#e6db74">{</span>file_name<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
</span></span></code></pre></div><ul>
<li>Cette boucle passe en revue tous les fichiers dans le répertoire spécifié, en vérifiant les fichiers avec l&rsquo;extension <code>.pdf</code>. Elle utilise <code>sorted()</code> pour les traiter dans l&rsquo;ordre alphabétique.</li>
</ul>
<p><strong>6. Lire chaque PDF et ajouter les pages au Writer</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>reader <span style="color:#f92672">=</span> PdfReader(file_path)
</span></span><span style="display:flex;"><span>writer<span style="color:#f92672">.</span>append(reader)
</span></span></code></pre></div><ul>
<li>Pour chaque PDF, <code>PdfReader</code> lit le fichier, puis toutes les pages de ce PDF sont ajoutées à <code>writer</code>.</li>
</ul>
<p><strong>7. Écrire le PDF fusionné dans un fichier de sortie</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>output_path <span style="color:#f92672">=</span> os<span style="color:#f92672">.</span>path<span style="color:#f92672">.</span>join(directory, output_file)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">with</span> open(output_path, <span style="color:#e6db74">&#34;wb&#34;</span>) <span style="color:#66d9ef">as</span> output_pdf:
</span></span><span style="display:flex;"><span>    writer<span style="color:#f92672">.</span>write(output_pdf)
</span></span></code></pre></div><ul>
<li>Après avoir collecté toutes les pages, <code>writer.write()</code> écrit le PDF fusionné sur le chemin de sortie spécifié.</li>
</ul>
<p><strong>8. Imprimer la confirmation</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Merged PDF saved as: </span><span style="color:#e6db74">{</span>output_path<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
</span></span></code></pre></div><ul>
<li>Imprime un message de succès confirmant l&rsquo;emplacement du fichier PDF fusionné sauvegardé.</li>
</ul>
<h3 id="fichiers-pdf-dentrée-utilisés-dans-le-code-et-pdf-de-sortie-fusionné">Fichiers PDF d&rsquo;entrée utilisés dans le code et PDF de sortie fusionné</h3>
<ul>
<li><strong>Fichiers PDF d&rsquo;entrée :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdfs-to-merge">Lien de téléchargement</a></li>
<li><strong>PDF de sortie fusionné :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdfs-to-merge/output-dir">Lien de téléchargement</a></li>
</ul>
<h2 id="4-diviser-un-pdf-en-utilisant-python">4. Diviser un PDF en utilisant Python</h2>
<script type="application/javascript" src="https://gist.github.com/fileformat-blog-gists/0dee64422ac0dcf44cf027d90567bbf8.js?file=split-pdf-using-pypdf-in-python.py"></script>

<h3 id="explication-du-code-3">Explication du Code</h3>
<p>Le script Python ci-dessus divise un PDF en pages séparées en utilisant la bibliothèque <strong>PyPDF</strong>. Il s&rsquo;assure d&rsquo;abord que le répertoire de sortie existe, puis lit le fichier PDF d&rsquo;entrée. Le script parcourt chaque page, crée un nouvel objet <strong>PdfWriter</strong>, et enregistre chaque page en tant que fichier PDF individuel. Les fichiers de sortie sont nommés séquentiellement (ex. <strong>page_1.pdf, page_2.pdf</strong>) et stockés dans le dossier <strong><code>output-dir</code></strong>. Enfin, il imprime un message de confirmation pour chaque fichier créé et notifie lorsque le processus est terminé.</p>
<h3 id="fichier-pdf-dentrée-et-fichiers-de-sortie-divisés">Fichier PDF d&rsquo;entrée et fichiers de sortie divisés</h3>
<ul>
<li><strong>Fichier PDF d&rsquo;entrée :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-split">Lien de téléchargement</a></li>
<li><strong>Fichiers de sortie divisés :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-split/output-dir">Lien de téléchargement</a></li>
</ul>
<h2 id="5-ajouter-un-filigrane-à-un-pdf-en-utilisant-python">5. Ajouter un filigrane à un PDF en utilisant Python</h2>
<p>Vous pouvez ajouter un filigrane à un PDF en utilisant la bibliothèque PyPDF en superposant un PDF de filigrane sur un PDF existant. Assurez-vous que le PDF de filigrane ne comporte qu&rsquo;une seule page pour qu&rsquo;il s&rsquo;applique correctement à chaque page du PDF principal.</p>
<script type="application/javascript" src="https://gist.github.com/fileformat-blog-gists/af057943580e2fcde6a635df34d7e39a.js?file=watermark-pdf-using-pypdf-in-python.py"></script>

<h3 id="explication-du-code-4">Explication du Code</h3>
<p>Le script Python ci-dessus lit un PDF d&rsquo;entrée, extrait un PDF de filigrane d&rsquo;une seule page, superpose le filigrane sur chaque page du PDF d&rsquo;entrée, et sauvegarde le PDF final avec le filigrane.</p>
<p><strong>Détail du Code</strong></p>
<p>Voici une brève explication de chaque partie</p>
<p><strong>1. Importer les classes requises</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> pypdf <span style="color:#f92672">import</span> PdfReader, PdfWriter
</span></span></code></pre></div><ul>
<li><strong><code>PdfReader</code></strong> est utilisé pour lire les PDF existants.</li>
<li><strong><code>PdfWriter</code></strong> est utilisé pour créer et écrire un nouveau PDF.</li>
</ul>
<p><strong>2. Définir les chemins de fichiers</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>input_pdf <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;pdf-to-watermark/input.pdf&#34;</span>
</span></span><span style="display:flex;"><span>watermark_pdf <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;pdf-to-watermark/watermark.pdf&#34;</span>
</span></span><span style="display:flex;"><span>output_pdf <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;pdf-to-watermark/output_with_watermark.pdf&#34;</span>
</span></span></code></pre></div><ul>
<li><code>input_pdf</code>: Le PDF original auquel le filigrane sera ajouté.</li>
<li><code>watermark_pdf</code>: Un PDF distinct d&rsquo;<strong>une page</strong> servant de filigrane.</li>
<li><code>output_pdf</code>: Le fichier de sortie qui contiendra les pages filigranées.</li>
</ul>
<p><strong>3. Lire les PDF</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>reader <span style="color:#f92672">=</span> PdfReader(input_pdf)
</span></span><span style="display:flex;"><span>watermark <span style="color:#f92672">=</span> PdfReader(watermark_pdf)
</span></span></code></pre></div><ul>
<li><code>reader</code>: Lit le PDF d&rsquo;entrée.</li>
<li><code>watermark</code>: Lit le PDF de filigrane.</li>
</ul>
<p><strong>4. Créer un objet Writer</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>writer <span style="color:#f92672">=</span> PdfWriter()
</span></span></code></pre></div><ul>
<li>Ceci sera utilisé pour créer le PDF final avec filigrane.</li>
</ul>
<p><strong>5. Extraire la page du filigrane</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>watermark_page <span style="color:#f92672">=</span> watermark<span style="color:#f92672">.</span>pages[<span style="color:#ae81ff">0</span>]
</span></span></code></pre></div><ul>
<li>On suppose que le PDF de filigrane comprend uniquement <strong>une page</strong>, qui est utilisée pour la surimpression sur toutes les pages.</li>
</ul>
<p><strong>6. Boucle à travers les pages PDF d&rsquo;entrée &amp; fusion du filigrane</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">for</span> page <span style="color:#f92672">in</span> reader<span style="color:#f92672">.</span>pages:
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># Fusionner le filigrane avec la page actuelle</span>
</span></span><span style="display:flex;"><span>    page<span style="color:#f92672">.</span>merge_page(watermark_page)
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># Ajouter la page fusionnée au writer</span>
</span></span><span style="display:flex;"><span>    writer<span style="color:#f92672">.</span>add_page(page)
</span></span></code></pre></div><ul>
<li>Itère à travers chaque page de <code>input_pdf</code>.</li>
<li><strong><code>merge_page(watermark_page)</code></strong> superpose le filigrane sur la page actuelle.</li>
<li>Ajoute la page modifiée au <code>writer</code>.</li>
</ul>
<p><strong>7. Sauvegarder le PDF avec filigrane</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">with</span> open(output_pdf, <span style="color:#e6db74">&#34;wb&#34;</span>) <span style="color:#66d9ef">as</span> output_file:
</span></span><span style="display:flex;"><span>    writer<span style="color:#f92672">.</span>write(output_file)
</span></span></code></pre></div><ul>
<li>Écrit les pages modifiées dans un nouveau fichier PDF.</li>
</ul>
<p><strong>8. Imprimer la confirmation</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Watermarked PDF saved as: </span><span style="color:#e6db74">{</span>output_pdf<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
</span></span></code></pre></div><ul>
<li>Affiche le chemin du fichier de sortie pour confirmation.</li>
</ul>
<h3 id="fichier-pdf-dentrée-fichier-pdf-de-filigrane-et-fichier-pdf-avec-filigrane-en-sortie">Fichier PDF d&rsquo;entrée, fichier PDF de filigrane, et fichier PDF avec filigrane en sortie</h3>
<ul>
<li><strong>Fichier PDF d&rsquo;entrée :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-watermark">Lien de téléchargement</a></li>
<li><strong>Fichier PDF de filigrane :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-watermark">Lien de téléchargement</a></li>
<li><strong>Fichier PDF avec filigrane en sortie :</strong> <a href="https://github.com/fileformat-blog-gists/code/tree/main/working-with-pdf-files-in-python/pdf-to-watermark">Lien de téléchargement</a></li>
</ul>
<p><strong>Capture d&rsquo;écran</strong>
<img loading="lazy" src="https://raw.githubusercontent.com/fileformat-blog-gists/content/main/working-with-pdf-files-in-python/watermark-pdf.png" alt="Capture d&amp;rsquo;écran d&amp;rsquo;un PDF avec filigrane en utilisant Python"  />
</p>
<h2 id="conclusion">Conclusion</h2>
<p>Dans ce guide, nous avons exploré les opérations PDF essentielles en Python, y compris l&rsquo;extraction de texte, la rotation des pages, la fusion, la division, et l&rsquo;ajout de filigranes. Avec ces compétences, vous pouvez maintenant créer votre propre gestionnaire de PDF et automatiser diverses tâches PDF efficacement.</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
