{"id":13656,"date":"2024-02-06T10:00:00","date_gmt":"2024-02-06T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=13656"},"modified":"2024-02-05T18:52:25","modified_gmt":"2024-02-05T09:52:25","slug":"adding-watermarks-to-pdfs-in-python-with-simple-and-efficient-approach","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/13656","title":{"rendered":"Adding Watermarks to PDFs in Python&nbsp;with&nbsp;simple and efficient approach"},"content":{"rendered":"\n<p>In today&#8217;s digital age, the need to protect and personalize PDF documents is more crucial than ever. Whether you want to brand your documents or add a confidential watermark, Python provides a powerful and straightforward solution. In this blog, we&#8217;ll explore a simple Python script that utilizes the PyPDF2 and ReportLab libraries to effortlessly add watermarks to multiple PDF files.<\/p>\n\n\n\n<p><strong>Setting Up the Environment<\/strong><\/p>\n\n\n\n<p>Before diving into the script, make sure you have the required libraries installed. You can do this by running the following commands.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install PyPDF2\npip install reportlab<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Understanding the Script<\/strong><\/p>\n\n\n\n<p>The entire script can be seen at the end. Let&#8217;s break down the key components of the script.<\/p>\n\n\n\n<p>1. create_watermark() Function<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&#8211; This function uses the ReportLab library to generate a PDF containing a customizable watermark.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&#8211; You can specify the watermark text, color, transparency, font, and rotation angle.<\/p>\n\n\n\n<p>2. add_watermark() Function<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&#8211; The core function that adds the watermark to each page of the input PDF.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&#8211; It uses PyPDF2 to merge the original PDF with the watermark PDF on each page.<\/p>\n\n\n\n<p>3. delete_watermark_file() Function<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&#8211; A utility function to delete the temporary watermark PDF file after it has been merged with the input files.<\/p>\n\n\n\n<p>4. Command Line Arguments<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&#8211; The script accepts two command line arguments:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; `&#8211;path`: The path to the directory containing the PDF files to watermark.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; `&#8211;watermark_text`: The text to be used as the watermark.<\/p>\n\n\n\n<p>Here is the entire script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import PyPDF2\nimport argparse\nfrom reportlab.pdfgen import canvas\nfrom reportlab.lib.units import inch\nfrom reportlab.lib import colors\nimport os\n\ndef create_watermark(watermark_text, output_pdf):\n    pdf = canvas.Canvas(output_pdf)\n    pdf.translate(inch, inch) # move the current origin point(0,0) of the canvas by the current given horizontal and vertical distances\n    pdf.setFillColor(colors.red, alpha=0.3) # set the font color with alpha value to adjust the transparency of watermark text\n    pdf.setFont(\"Helvetica\", 50) # set font and font size\n    pdf.rotate(45) # rotate the canvas by 45 degrees\n    pdf.drawCentredString(400, 100, watermark_text) # center the watermark text\n    pdf.save()\n\ndef add_watermark(input_pdf, output_directory, watermark_pdf):\n    base_filename = os.path.splitext(os.path.basename(input_pdf))&#091;0]\n    output_pdf = os.path.join(output_directory, f'{base_filename}.pdf')\n    with open(input_pdf, 'rb') as file:\n        pdf_reader = PyPDF2.PdfReader(file)\n        pdf_writer = PyPDF2.PdfWriter()\n\n        for page_num in range(len(pdf_reader.pages)):\n            page = pdf_reader.pages&#091;page_num]\n            watermark_reader = PyPDF2.PdfReader(watermark_pdf)\n            watermark_page = watermark_reader.pages&#091;0]\n            page.merge_page(watermark_page)\n            pdf_writer.add_page(page)\n\n        with open(output_pdf, 'wb') as output_file:\n            pdf_writer.write(output_file)\n\ndef delete_watermark_file(watermark_pdf):\n    if os.path.exists(watermark_pdf):\n        os.remove(watermark_pdf)\n        \nif __name__ == '__main__':\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\"--path\", required=True, type=str, help=\"Path of the directory of file lists\")\n    parser.add_argument(\"--watermark_text\", required=True, type=str, help=\"Text to be watermark\")\n    \n    args = parser.parse_args()\n\n    path = args.path\n    watermark_text = args.watermark_text\n    \n    print('Processing.....')\n    file_list = &#091;file_name for file_name in os.listdir(path) if os.path.isfile(os.path.join(path, file_name))]\n\n    output_directory = os.path.join(path, 'output_directory')\n    if not os.path.exists(output_directory):\n        os.makedirs(output_directory)\n            \n    watermark_pdf_file = os.path.join(path, 'watermark.pdf')\n    create_watermark(watermark_text, watermark_pdf_file)\n    for file_name in file_list:\n        if not file_name == 'watermark.pdf':\n            input_pdf_file = os.path.join(path, file_name)\n            add_watermark(input_pdf_file, output_directory, watermark_pdf_file)\n            \n    delete_watermark_file(watermark_pdf_file)\n    print('Done!')<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Running the Script<\/strong><\/p>\n\n\n\n<p>To run the script, execute the following command.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python script_name.py --path \/path\/to\/pdf\/files --watermark_text \"Your Watermark Text\"<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>The script will process each PDF file in the specified directory, add the watermark, and save the watermarked files in a newly created &#8216;output_directory.&#8217;<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>With this Python script, you can easily add watermarks to your PDF documents, making them visually distinctive and secure. Whether you&#8217;re protecting sensitive information or branding your documents, this solution provides a quick and efficient way to enhance your PDF files. I would recommend to look into the used libraries in details and feel free to customize the script further to suit your specific requirements, such as adjusting colors, fonts, or rotation angles for the watermark.<\/p>\n\n\n\n<p>Ref: <a href=\"https:\/\/pypdf2.readthedocs.io\/en\/3.0.0\/index.html\"><u>https:\/\/pypdf2.readthedocs.io\/en\/3.0.0\/index.html<\/u><\/a><\/p>\n\n\n\n<p>Ref: <a href=\"https:\/\/docs.reportlab.com\"><u>https:\/\/docs.reportlab.com<\/u><\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13656\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/13656\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Adding Watermarks to PDFs in Python&nbsp;with&nbsp;simple and efficient approach\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13656\" data-text=\"Adding Watermarks to PDFs in Python&nbsp;with&nbsp;simple and efficient approach\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13656\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13656\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s digital age, the need to protect and personalize PDF documents is more crucial than ever. Whe [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100,27],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13656"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=13656"}],"version-history":[{"count":4,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13656\/revisions"}],"predecessor-version":[{"id":13661,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13656\/revisions\/13661"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=13656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=13656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=13656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}