Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17419

Replacing google slides images using slides api

$
0
0

I have the following scripts to update the images in google slides, it works fine but sometimes it gives me 400 error, service account has a write permission to the drive/images and the presentation

image_mappings = {"id_in_presenation": "name_of_the_image","id_in_presenation1": "name_of_the_image1",}# listing .png files def get_png_files(drive_folder_id, drive_service):    query = f"'{drive_folder_id}' in parents and mimeType='image/png'"    results = drive_service.files().list(q=query, supportsAllDrives=True, fields="files(id, name, webViewLink, webContentLink)").execute()    drive_png_files = results.get('files', [])    print('drive_png_files', drive_png_files)    return drive_png_files`# updating images in slidesdef update_images_in_slides(drive_png_files, slides_service, presentation_id):"""    Updates images in a Google Slides presentation.    Args:    drive_png_files: A list of .png files from Google Drive.    slides_service: The authenticated Google Slides service object.    presentation_id: The ID of the presentation to update."""    requests = []    for slide_image_id, new_image_name in info.image_mappings.items():        drive_file = next((item for item in drive_png_files if item['name'] == new_image_name), None)        if drive_file:            requests.append({'replaceImage': {'imageObjectId': slide_image_id,'imageReplaceMethod': 'CENTER_INSIDE','url': f'https://drive.google.com/uc?id={drive_file["id"]}'                }            })    if requests:        body = {'requests': requests}        try:            response = slides_service.presentations().batchUpdate(presentationId=presentation_id, body=body).execute()            time.sleep(10)            print(f"\nUpdated the slide: {response}")        except Exception as exc:            raise ValueError(f"Failed to update the presentation.") from exc

gave editor and writer access permission to the service account for the drives, images, presentation did not help


Viewing all articles
Browse latest Browse all 17419

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>