colab pytoncode: how to convert pdf to blogspot images

# ============================================
#  PDF → Images → Blogspot HTML (Colab)
# ============================================

# 1. Install required packages
!apt-get update -qq
!apt-get install -y -qq poppler-utils
!pip install -q pdf2image pillow

from pdf2image import convert_from_path
from PIL import Image
import base64
import os
from IPython.display import display, HTML, Image as IPImage

# 2. Convert PDF pages to images
PDF_PATH = "/content/in.pdf"          # <-- make sure you uploaded in.pdf here
OUTPUT_DIR = "/content/pdf_images"
os.makedirs(OUTPUT_DIR, exist_ok=True)

print("Converting PDF pages to images...")
pages = convert_from_path(PDF_PATH, dpi=150)   # increase dpi for higher quality

image_files = []
for i, page in enumerate(pages, start=1):
    img_path = os.path.join(OUTPUT_DIR, f"image{i}.png")
    page.save(img_path, "PNG")
    image_files.append(img_path)
    print(f"  Saved → {img_path}")

print(f"\nTotal pages converted: {len(image_files)}")

# 3. Preview the images inside Colab (optional)
print("\n--- Preview of pages ---")
for img_path in image_files:
    display(IPImage(filename=img_path, width=400))

# 4. Generate Blogspot-ready HTML (images embedded as base64)
def image_to_base64(path):
    with open(path, "rb") as f:
        return base64.b64encode(f.read()).decode("utf-8")

html_parts = [
    '<!-- ===== Blogspot / Blogger HTML (paste in HTML view) ===== -->',
    '<div style="text-align:center; max-width:100%; margin:0 auto;">'
]

for i, img_path in enumerate(image_files, start=1):
    b64 = image_to_base64(img_path)
    html_parts.append(
        f'<img src="data:image/png;base64,{b64}" '
        f'alt="Page {i}" '
        f'style="max-width:100%; height:auto; margin:15px 0; border:1px solid #ddd; border-radius:4px;" />'
    )

html_parts.append('</div>')
html_code = "\n".join(html_parts)

# 5. Save the HTML file and also print it
html_file = "/content/blogspot_post.html"
with open(html_file, "w", encoding="utf-8") as f:
    f.write(html_code)

print("\n" + "="*60)
print("HTML file saved to:", html_file)
print("="*60)
print("\nYou can download 'blogspot_post.html' from the file browser on the left.")
print("Or copy the HTML below and paste it into Blogspot → Create post → HTML view:\n")

# Show a short preview of the HTML (first 1500 chars)
print(html_code[:1500] + "\n... (truncated for display) ...")
print(f"\nFull HTML length: {len(html_code)} characters")
----------------------

Here’s a ready-to-run Google Colab script that:

  1. Reads /content/in.pdf
  2. Converts every page into an image (image1.png, image2.png, …)
  3. Generates a self-contained HTML snippet (with base64 images) that you can paste directly into a Blogger / Blogspot post
  4. How to use

    1. Open a new Colab notebook.
    2. Upload your PDF and rename it to in.pdf (or change the path in the code).
    3. Run the whole cell.
    4. After it finishes:
      • All page images are saved in /content/pdf_images/image1.png, image2.png, …
      • A ready-to-paste HTML file is created: /content/blogspot_post.html
    5. Download blogspot_post.html, open it, copy everything, then in Blogspot:
      • Create a new post
      • Switch to HTML view
      • Paste the code
      • Publish

    The images are embedded as base64, so you don’t need to upload any files separately — the whole post is self-contained.

Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

SAP CPI camel conditions and xpath conditions

chatgpt explanation: all retry messages in sap cpi, with example using jms sender.