How do I insert images into a Google Sheet in Python using gspread (or any other package)? - image

I need a way to insert an image into a specific cell in a specific Google Sheet. If there is any python package and/or slice of code that can accomplish this, please let me know. As far as I am aware Gspread does not have anything helpful.

You can simply insert an image from an URL with a formula like this:
'=image("https://drive.google.com/uc?export=view&id={}")'.format(file_id)
When you insert/upload the data be sure to choose USER_ENTERED for value input option. https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
With gspred:
sheet_instance.insert_rows(rows, value_input_option='USER_ENTERED')

Overall to add image into Google Sheets can be accomplished with Python by first inserting the image file in Google Drive using the Google Drive API:
Here is how to insert it to google drive. - https://developers.google.com/drive/v2/reference/files/insert#examples,
and secondly linking to the image in Google Sheets with the Google Sheets API, Here is Google sheets API - https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#CellData
Note: Look under CellData

Related

Parsing Youtube - Xpath to retrieve a Youtube Channel's Profile Picture and Description

I would like to import the profile picture of a number of Youtube Channels into a Google Sheets spreadsheet.
This is the information that I have been able to retrieve so far:
To retrieve the profile picture, I have used the following code using Social Blade's website:
=IMPORTXML(D2,"//img[#id='YouTubeUserTopInfoAvatar']")
However while it doesn't result in an erorr it doesn't retrieve the image from the src. Any idea how to make it work?
In regard to the description, I would like to use a code that can be consistently used using either the channel's id or Youtube's username. Other solutions that I have tried have allowed me to get a truncated description, not the full one.
Maybe there is a more cost-effective way of doing this task, but without much Javascript experience this is the easiest way I have found to retrieve the information. Ideally, I would like to find a solution that calls on the Youtube API and bypasses the 50-call limit.
Here's a sample Google Sheets template to work on a solution.
OK. I've edited your Google Sheet. You forgot the #src attribute for the image.
=IMAGE(IMPORTXML(E2;"//img[#id='YouTubeUserTopInfoAvatar']/#src"))
And I've added the XPath for the Youtube channel description (add "/about" at the end of the youtube channel links).
=IMPORTXML(C2,"//div[#class='about-description branded-page-box-padding']")
Note : You have to fix your "ChannelID" formula to extract the ID.

How Do I Import Data Into My Google Sheet from a Website Using importXML

Today when experimenting with using importXML in Google Sheets, I ran into a problem. I was attempting to import the title header of a USTA Tournament page into the Google Sheet, however, this did not work as it just resulted in the HTML title of the webpage being displayed ('TournamentHome'). Below is the Google Sheet, and the website that is used:
Google Sheet and Function:
=importXML(F2, "//html//body[#id='thebody']//div[#id='content']//div[#id='pagetitle']")
Website and Section of Source Code Being Used
The title that I am trying to extract from the website is TOWPATH 24th ANNUAL THANKSGIVING JR SINGLES.
The link to the website is https://m.tennislink.usta.com/tournamenthome?T=225779
update:
=REGEXEXTRACT(QUERY(ARRAY_CONSTRAIN(IMPORTDATA(
"https://m.tennislink.usta.com/tournamenthome?T=225779"), 555, 1),
"where Col1 contains 'escape'"), "\(""(.*)""\)")
unfortunately, that won't be possible the way you trying because the field you attempt to scrape is controlled by JavaScript and Google Sheets can't understand/import JS. you can test this simply by disabling JS for a given link and you will see what exactly can be imported into Google Sheets:
How about this sample formula? In this formula, the title value is directly retrieved from the script before the value is put to #pagetitle. Please think of this as just one of several answers.
Sample formula:
=REGEXEXTRACT(IMPORTXML(A1,"//div[#class='tournament_search']/script"),"escape\(""([\w\s\S]+)""")
Result:
When https://m.tennislink.usta.com/TournamentHome/tournament.aspx?T=38079 and https://m.tennislink.usta.com/tournamenthome?T=225779 are put in "A1" and "A2", the results are as follows.
Reference:
REGEXEXTRACT

Import data from ebay to google spreadsheet using IMPORTXML

I'm trying to Import a table from "https://www.ebay.com/itm/100-NEW-ALTERNATOR-VW-GOLF-GTI-GL-GLS-1-8T-1-8-2L-99-06-90A-1-YR-WARRANTY-13852/301364941754?fits=Model%3AJetta&hash=item462ac013ba:g:v7oAAOSw~YRagU4N&vxp=mtr" to a Google Spread sheet using =IMPORTXML function, The Formula I was using as below,
A1 = https://www.ebay.com/itm/100-NEW-ALTERNATOR-VW-GOLF-GTI-GL-GLS-1-8T-1-8-2L-99-06-90A-1-YR-WARRANTY-13852/301364941754?fits=Model%3AJetta&hash=item462ac013ba:g:v7oAAOSw~YRagU4N&vxp=mtr
A2 = //*[#id="w1-20ctbl"]
A3 = =IMPORTXML(A1,A2)
But it returns nothing and It says "Imported content is empty."
Can Somebody help me, I'm new to google sheet scripting and I'll really grateful if somebody can help me.
waiting to hearing from somebody....
Thanks
You cannot access the pictured table using IMPORTXML or any built-in Google Sheets formula because the table is generated when a user visits the website.
If you look at the page source, you'll see that the table does not exist. IMPORTPATH looks at this page source, which is the content before javascript rendering by the browser. When you "inspect" an element in your browser, it's inspecting the content after the javascript has been rendered.
Unfortunately, there is not a simple way to get the data you're looking for. You'll have to find or build your own scraping tool. Be careful not to violate eBay terms of use or any local laws.

Pass an image get a list of URLs matching the image, HOW?

I'm essentially trying to do a reverse image search, i.e. I want to pass in an image and get back a results list of instances on the web where that image is found. I know Google's old API that did this is depreciated, I see some answers on SO (e.g. Google custom search for images only) that talk about doing an image search with Google's Custom Search API, but every time I dig into the code they are retrieving images from a string rather than what I'm trying to do. Is there currently any API that will help me with what I'm trying to do?
I'm sorry. I cannot write comments yet. How about this? https://github.com/tanaikech/goris
Recently, I found this. I don't know whether this is what you want.

Loading the first 5 images from google with specific keyword

I'm trying to load the first 5 image that comes up on google when I type a given keyword in my app. So let's say if the keyword was "Butter" I want to load the first 5 images that com up on google if you type butter.
I've been looking at the Github project SDWebImages (https://github.com/rs/SDWebImage), but it looks like you can only load the images if you have the url of the image.
Anybody know how I can do what I described, or anybody that can point me in the right direction as to what I should look at to do it.
Google has deprecated their image search API and you are now supposed to use custom search which supports images. You will need to sign up for an API key. When you make your search request you need to set your searchType parameter to image.

Resources