Last modified: Jan 11, 2025 By Alexander Williams
Python PdfWriter.add_metadata: Add PDF Metadata
Adding metadata to PDF files is essential for organizing and managing documents. Python's PdfWriter.add_metadata method makes it easy to add metadata to your PDFs.
This article will guide you through the process of using PdfWriter.add_metadata
to add metadata to your PDF files. We'll also provide examples and code snippets to help you get started.
What is PdfWriter.add_metadata?
The PdfWriter.add_metadata
method is part of the PyPDF2 library. It allows you to add metadata to a PDF file. Metadata includes information like the title, author, subject, and keywords.
This method is useful when you need to add or update metadata in a PDF file programmatically. It can be used in various applications, such as document management systems.
How to Use PdfWriter.add_metadata
To use PdfWriter.add_metadata
, you first need to install the PyPDF2 library. You can install it using pip:
pip install PyPDF2
Once installed, you can start adding metadata to your PDF files. Here's a step-by-step guide:
Step 1: Import the Required Libraries
First, import the necessary libraries. You'll need PyPDF2 to work with PDF files.
import PyPDF2
Step 2: Open the PDF File
Next, open the PDF file you want to add metadata to. You can use the PdfReader
class to read the file.
reader = PyPDF2.PdfReader("example.pdf")
Step 3: Create a PdfWriter Object
Create a PdfWriter
object to write the metadata to the PDF file.
writer = PyPDF2.PdfWriter()
Step 4: Add Pages to the Writer
Add the pages from the original PDF to the PdfWriter
object.
for page in reader.pages:
writer.add_page(page)
Step 5: Add Metadata
Now, use the add_metadata
method to add metadata to the PDF file. You can specify the title, author, subject, and keywords.
metadata = {
"/Title": "Example PDF",
"/Author": "John Doe",
"/Subject": "Sample PDF with Metadata",
"/Keywords": "PDF, Python, Metadata"
}
writer.add_metadata(metadata)
Step 6: Save the PDF File
Finally, save the modified PDF file with the new metadata.
with open("example_with_metadata.pdf", "wb") as output_pdf:
writer.write(output_pdf)
Example Code and Output
Here's the complete example code for adding metadata to a PDF file:
import PyPDF2
# Open the PDF file
reader = PyPDF2.PdfReader("example.pdf")
# Create a PdfWriter object
writer = PyPDF2.PdfWriter()
# Add pages to the writer
for page in reader.pages:
writer.add_page(page)
# Add metadata
metadata = {
"/Title": "Example PDF",
"/Author": "John Doe",
"/Subject": "Sample PDF with Metadata",
"/Keywords": "PDF, Python, Metadata"
}
writer.add_metadata(metadata)
# Save the PDF file
with open("example_with_metadata.pdf", "wb") as output_pdf:
writer.write(output_pdf)
After running the code, you'll have a new PDF file named example_with_metadata.pdf
with the added metadata.
Conclusion
Adding metadata to PDF files is a simple yet powerful way to organize and manage your documents. With Python's PdfWriter.add_metadata
method, you can easily add metadata to your PDFs programmatically.
This guide has walked you through the process of using PdfWriter.add_metadata
to add metadata to a PDF file. We hope you found it helpful!
For more information on working with PDFs in Python, check out our articles on Extract Text from PDFs with Python PdfReader and Python PdfReader.getDocumentInfo: Extract PDF Metadata.