Using PyAutoGUI to take screenshot and save into PDF and looping it 5 times

Viewed 26

I'm a newbie using Python, i would like to use python PyAutoGui to do the below steps:

  1. take a screenshot
  2. save the screenshot as PNG into a PDF
  3. click on next page
  4. repeat step 1 to 3 and keep append the PNG file into the same PDF for 5 times

Hope someone can help me to amend my script. Thank you.

import pyautogui
from PIL import Image


#Take Screenshot.

my_screenshot = pyautogui.screenshot()
screenshot_path = r'C:\Users\Eve\Desktop\test\SS.png'
my_screenshot.save(screenshot_path)

image_1 = Image.open(screenshot_path)
im_1 = image_1.convert('RGB')

#save screenshot into pdf and append the other screenshot into the same pdf.

pdf_path = r'C:\Users\Eve\Desktop\test\New PDF.pdf'
im_1.save(pdf_path)

#Move mouse to click on next page to screenshot

pyautogui.moveTo(1333,657, duration=1)
pyautogui.click(1333,657, duration=1)
1 Answers

Let us first save all the photos like this: 1,2,3,4, & 5.

for i in range(1, 6): 
    my_screenshot = pyautogui.screenshot()
    screenshot_path = r'C:\\Users\\Eve\\Desktop\\test\\' + str(i) + '.png'
    my_screenshot.save(screenshot_path)

    pyautogui.moveTo(1333,657, duration=1)
    pyautogui.click(1333,657, duration=1)

Considering you are saving 5 photos with numbers like 1,2,3,4, & 5, we will now save them as PDF:

image_list = []
for i in range(1, 6):
    print(i)
    image_list.append(
        Image.open(r'C:\\Users\\Eve\\Desktop\\test\\' + str(i) + '.png').convert('RGB')
    )

image_list[0].save('images.pdf', save_all=True, append_images=image_list[1:])

Using this code you will be able to get a pdf with 3 images.

Related