Untitled
unknown
plain_text
10 months ago
1.5 kB
21
Indexable
from PyPDF2 import PdfReader
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from xml.sax.saxutils import escape
import re
# Input and output file paths
input_pdf = "GmailSherlock - 1.pdf"
output_pdf = "CleanedMail.pdf"
# Read the original PDF
reader = PdfReader(input_pdf)
styles = getSampleStyleSheet()
content = []
for page in reader.pages:
text = page.extract_text()
if not text:
continue
cleaned_lines = []
skip_block = False
for line in text.split("\n"):
# Remove [Quoted text hidden]
if "[Quoted text hidden]" in line:
continue
# Detect start of Gmail-style quoted reply
if re.match(r"^On .* wrote:$", line.strip()):
skip_block = True
continue
# Skip lines while inside quoted block
if skip_block:
if line.strip() == "":
skip_block = False
continue
cleaned_lines.append(line)
cleaned_text = "\n".join(cleaned_lines)
# Add cleaned text to PDF story
for para in cleaned_text.split("\n"):
if para.strip(): # skip empty lines
safe_text = escape(para) # escape special characters
content.append(Paragraph(safe_text, styles["Normal"]))
# Write the cleaned PDF
pdf = SimpleDocTemplate(output_pdf)
pdf.build(content)
print(f"Cleaned PDF saved as {output_pdf}")
Editor is loading...
Leave a Comment