Skip to content

Commit 07d02c3

Browse files
committed
add pdf metadata remover tutorial
1 parent d97902b commit 07d02c3

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
6262
- [How to Implement 2FA in Python](https://thepythoncode.com/article/implement-2fa-in-python). ([code](ethical-hacking/implement-2fa))
6363
- [How to Build a Username Search Tool in Python](https://thepythoncode.com/code/social-media-username-finder-in-python). ([code](ethical-hacking/username-finder))
6464
- [How to Find Past Wi-Fi Connections on Windows in Python](https://thepythoncode.com/article/find-past-wifi-connections-on-windows-in-python). ([code](ethical-hacking/find-past-wifi-connections-on-windows))
65+
- [How to Remove Metadata from PDFs in Python](https://thepythoncode.com/article/how-to-remove-metadata-from-pdfs-in-python). ([code](ethical-hacking/pdf-metadata-remover))
6566

6667
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
6768
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Remove Metadata from PDFs in Python](https://thepythoncode.com/article/how-to-remove-metadata-from-pdfs-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import PyPDF2
2+
3+
def remove_metadata(pdf_file):
4+
# Open the PDF file.
5+
with open(pdf_file, 'rb') as file:
6+
reader = PyPDF2.PdfReader(file)
7+
8+
# Check if metadata exists.
9+
if reader.metadata is not None:
10+
print("Metadata found in the PDF file.")
11+
12+
# Create a new PDF file without metadata.
13+
writer = PyPDF2.PdfWriter()
14+
15+
# Copy pages from the original PDF to the new PDF.
16+
for page_num in range(len(reader.pages)):
17+
page = reader.pages[page_num]
18+
writer.add_page(page)
19+
20+
# Open a new file to write the PDF without metadata.
21+
new_pdf_file = f"{pdf_file.split('.')[0]}_no_metadata.pdf"
22+
with open(new_pdf_file, 'wb') as output_file:
23+
writer.write(output_file)
24+
25+
print(f"PDF file without metadata saved as '{new_pdf_file}'.")
26+
else:
27+
print("No metadata found in the PDF file.")
28+
29+
# Specify the path to your PDF file.
30+
pdf_file_path = "EEE415PQ.pdf"
31+
32+
# Call the function to remove metadata.
33+
remove_metadata(pdf_file_path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyPDF2==3.0.1

0 commit comments

Comments
 (0)