3

I need to take a screen shot of a specific range (eg: B2:Z15) in a Google sheet whenever the value in a specific cell (eg A1) changes - using Python.

The product has to be an image file saved from the capture. Not sure which library can do this. Thanks for help.

ibexy
  • 609
  • 3
  • 16
  • 34
  • The best way to do it might actually be to generate an image from the data rather than trying to take a screenshot. (e.g make a text box for each row that you capture and stack those text boxes on each other) – Ryan Schaefer Feb 16 '21 at 16:01
  • If you just want to scrape data(not considering taking screen shot), try pandas – rish_hyun Feb 16 '21 at 16:27
  • Hi @RyanSchaefer I quite like your idea. Can python do this: ["make a text box for each row that you capture and stack those text boxes on each other"] – ibexy Feb 16 '21 at 16:28
  • @rish_hyun the requirement is to capture an image of the screen – ibexy Feb 16 '21 at 16:28
  • Do you know well in advance that how many entries are there in sheet which you want to capture? If the data fits on the screen without scrolling, you could take screenshot of entire screen using `pyautogui ` and then `opencv` could process the image accordingly – rish_hyun Feb 16 '21 at 16:34
  • Another option is using `selenium`,Selenium is basically used to automate the testing across various web browsers. So you can automate the task of performing operations such as select the cells and export it online (google sheet). Well, using this is too cumbersome – rish_hyun Feb 16 '21 at 16:37
  • If you use `pandas`, it could read the excel or csv file and meaningful data can be processed out of it and you can export it as image file too! Have a look at this https://stackoverflow.com/questions/35634238/how-to-save-a-pandas-dataframe-table-as-a-png – rish_hyun Feb 16 '21 at 16:39
  • 1
    Do you own the data in this Google sheet? What type of data is in A1? How often can the data in A1 change? How do you access this Google sheet? Is the location of this Google Sheet always the same on your screen? Where do you want to store the screenshot? What format of image do you want? I have many more questions.. – Life is complex Feb 18 '21 at 17:52
  • 1
    @Lifeiscomplex to answer your questions: [Q]Do you own the data in this Google sheet? [A] YES. [Q]What type of data is in A1? [A] An Integer that changes from time to time. [Q]Is the location of this Google Sheet always the same on your screen? [A] Yes. I assume the process will first maximise the screen? [A] Where do you want to store the screenshot? [A] local machine in a designated folder. [Q] What format of image do you want? [A] Any of .png, or .jpg [Q] I have many more questions. [A] go on ... :) – ibexy Feb 18 '21 at 18:11
  • 1
    @ibexy thanks for the details. This is a complex problem, which I haven't tried before, but I have interest in researching and "maybe" solving. – Life is complex Feb 18 '21 at 18:14
  • @Lifeiscomplex thanks for your interest. – ibexy Feb 18 '21 at 18:38
  • 1
    @ibexy do you are have the code that detects that A1 has changed? I'm asking, because there are a lot of moving parts to this question. – Life is complex Feb 18 '21 at 19:02
  • @Lifeiscomplex I already have code that can connect to and read values from the spreadsheet. I can monitor change from that. I just need to fire off the screen capture operation on change. Its the screen capture bit am stuck with. – ibexy Feb 18 '21 at 19:18
  • @ibexy that makes life somewhat easier. – Life is complex Feb 18 '21 at 19:19

1 Answers1

1

I'm just going to focus on grabbing a snapshot of the Google Sheet as you requested in your comments. I did some research and discovered a Python module called pyscreenshot. The screen coordinates in pyscreenshot.grab are from my system, so they have to be modified for your system.

import pyscreenshot

# im=pyscreenshot.grab(bbox=(x1,x2,y1,y2))
image = pyscreenshot.grab(bbox=(90, 240, 1500, 485))

# To view the screenshot
image.show()

# To save the screenshot 
image.save("screenshot_google_sheet_rows.png")

The image below shows a capture of cells B1:Z15. You can capture any visible location in the Google Sheet by modifying the values x1, x2, y1, y2.

enter image description here

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • `pyscreenshot` seems to only take a screenshot of the specified part of the screen, it cannot generate screenshot for Excel files without opening it first with Excel. – Teddy C Sep 27 '22 at 13:20
  • This is correct. I had manually opened Google Sheets prior to using `pyscreenshot` – Life is complex Sep 27 '22 at 15:46