Search code examples
python-3.xgoogle-slides-api

Changing table column width using Google-Slide-API and python


I have a Spreadsheet document and by using Google-Sheet-API I'm fetching the data from it. Then by using Google-Slide-API I'm creating a Slide document with 'n' slides. On each slide I'm creating a 2-column table with 'n' rows. Every first column on every slide always contains data from the Spreadsheet's A column. The second columns contain data from the other Spreadsheet's columns (from B to 'n'). Then I'm changing the text size. So far so good.

Now, when I try to adjust the columns width of each table on each slide it doesn't do anything which is an issue because some tables contain more information and therefore the tables don't fit the slide. This is the part of the code that doesn't work:

for i in range(number_of_slides):
    regs = [
        {'updateTableColumnProperties': {
            'objectId': tableID[i],
            'columnIndices': [j],
            'tableColumnProperties': {
            'columnWidth': {'magnitude': mag[j], 'unit': 'PT'}
                },
            'fields': 'columnWidth'
            }
         } for j in range(2) ]
     SLIDES.presentations().batchUpdate(body={'requests': reqs},
            presentationId=deckID).execute()

The tables always remain the same with or without this part so it doesn't have any affect whatsoever. The code doesn't return any errors or messages.


Solution

  • I believe your goal as follows.

    • You have a Google Slides.
    • The Google Slides has the several slides, each slide has a table which has 2 columns.
    • You want to change the column width of 2 columns.
    • You want to achieve this using googleapis for python.
    • You have already been able to use the batchUpdate method using Google Slides API.

    Modification points:

    • I think that your request body is correct. But, I would like to propose one modification point. In your script, batchUpdate method is used in a loop. I think that when batchUpdate is used, updateTableColumnProperties for all slides in a Google Slides can be run by one API call.

    Although I'm not sure about your values of mag and your whole script, as a sample script for achieving your above goal, how about the following modified script? If this helped you to understand Slides API, I'm glad.

    Modified script:

    About creds, please use your authorization script. Also, you can see it at Quickstart for python.

    service = build('slides', 'v1', credentials=creds)
    PRESENTATION_ID = '###' # Please set Google Slides ID.
    magnitude = [100, 300] # Please set the widths for the columns A and B in each table. In this sample, 100 and 300 PT are set.
    
    # 1. Retrieve an object for each slides.
    presentation = service.presentations().get(presentationId=PRESENTATION_ID).execute()
    
    # 2. Create a request body for the batchUpdate method.
    slides = presentation.get('slides')
    requests = []
    for slide in slides:
        pe = slide.get('pageElements')
        if pe:
            for pageElement in pe:
                t = pageElement.get('table')
                if t:
                    for i, m in enumerate(magnitude):
                        requests.append({
                            'updateTableColumnProperties': {
                                'objectId': pageElement['objectId'],
                                'columnIndices': [i],
                                'tableColumnProperties': {
                                    'columnWidth': {'magnitude': m, 'unit': 'PT'}
                                },
                                'fields': 'columnWidth'
                            }
                        })
    
    # 3. Request the request body.
    service.presentations().batchUpdate(body={'requests': requests}, presentationId=PRESENTATION_ID).execute()
    

    Note:

    • When above script is run, the widths of columns "A" and "B" of the table in each slide are modified. In this sample script, 100 and 300 PT are set for the columns "A" and "B", respectively. About this, please modify for your actual situation.

    References: