0

I am creating a dashboard, and I want to copy the window I created onto a PowerPoint presentation.

My problem with using Pillow or other modules is that they take only what's on the screen; however, my window is bigger than that, with a Y scrollbar.

Is there a way to just convert the whole window to a picture?

Something along the lines of:

# Creation of the window
root = tk.Tk()
# All the code to create it
root.mainloop()

root.convert_to_picture() #or something of that sort

....

# what i tried to do:
import tkinter as tk
import pyautogui
from pptx import Presentation
from pptx.util import Inches
import time

def capture_window_to_image(window, file_path):
    window.update()
    x = window.winfo_rootx()
    y = window.winfo_rooty()
    w = window.winfo_width()
    h = window.winfo_height()
    time.sleep(1)  # Ensure the window is fully drawn
    screenshot = pyautogui.screenshot(region=(x, y, w, h))
    screenshot.save(file_path)

def create_ppt_with_image(image_path, ppt_path):
    # Create a presentation object
    prs = Presentation()

    # Add a slide
    slide_layout = prs.slide_layouts[5]  # Use a blank slide layout
    slide = prs.slides.add_slide(slide_layout)

    # Insert the captured image
    left = Inches(1)
    top = Inches(1)
    height = Inches(4.5)  # Adjust the height as needed
    slide.shapes.add_picture(image_path, left, top, height=height)

    # Save the presentation
    prs.save(ppt_path)




capture_window_to_image(root, "tkinter_screenshot.png")

# Create the PowerPoint presentation with the captured image
create_ppt_with_image("tkinter_screenshot.png", "tkinter_to_ppt.pptx")


# I tried this but as I said, it only captures the some part of window.
2
  • Is there a reason you want to automate this as opposed to just taking screenshots of your GUI?
    – JRiggles
    Commented Jul 10 at 12:18
  • 1
    Yes, first of all the window is bigger than the normal screen, so taking a screenshot (even manually) will only capture half of the window, and i need the whole window in a single picture. Secondly because my code handles files in batches, and eventually it will create a pptx presentation with dozens of files. Automating it will be so much faster. Commented Jul 11 at 8:08

0