首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gdoctableapp模块在python中使用google创建表时,表启动位置错误

使用gdoctableapp模块在python中使用google创建表时,表启动位置错误
EN

Stack Overflow用户
提问于 2021-12-24 09:24:55
回答 1查看 124关注 0票数 0

我正在尝试使用google创建一个通用文档。我正在插入边框不可见的图像和动态表。Tanaike's solutionto create table by keeping borders invisible worked,但是在这里,我将所有请求附加到一个请求中,然后插入到文档中,但是在实现以下代码后会出现错误:

代码语言:javascript
复制
import io
from gdoctableapppy import gdoctableapp

SERVICE_FILENAME = 'C:/Users/XYZ/service_account.json'  # set path to service account filename

from googleapiclient.discovery import build
from google.oauth2 import service_account
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload

credentials = service_account.Credentials.from_service_account_file(SERVICE_FILENAME,
                                                                    scopes=['https://www.googleapis.com/auth/drive',
                                                                            'https://www.googleapis.com/auth/documents']
                                                                    )
docs = build('docs', 'v1', credentials=credentials)
drive = build('drive', 'v3', credentials=credentials)


def create_file(file_name):
    file_metadata = {
        "title": file_name,
        "body": {}
    }

    file = docs.documents().create(body=file_metadata).execute()
    print('File ID: %s' % file.get('documentId'))
    file_id = file.get('documentId')
    try:
        permission = {
            "role": "writer",
            "type": "user",
            'emailAddress': 'xyz@gmail.com'
        }
        result = drive.permissions().create(fileId=file_id, body=permission).execute()
        print(result)
        return file_id
    except Exception as e:
        print('An error occurred:', e)
    return None


def insert_data(file_id):
    requests = []
    requests.append(insert_image())
    requests.append(insert_text(2, '\n'))
    requests.append(insert_text(3, 'text'))
    values = [['Name of the Client/Organization', 'XYZ'], ['Industry', 'Software']]
    requests.append(insert_table_data(file_id, values))

    result = docs.documents().batchUpdate(documentId=file_id, body={'requests': requests}).execute()


def insert_image():
    image_data = {
        'insertInlineImage': {
            'location': {
                'index': 1
            },
            'uri':
                'https://www.oberlo.com/media/1603970279-pexels-photo-3.jpg?fit=max&fm=jpg&w=1824',
            'objectSize': {
                'height': {
                    'magnitude': 350,
                    'unit': 'PT'
                },
                'width': {
                    'magnitude': 350,
                    'unit': 'PT'
                }
            }

        }
    }
    return image_data


def insert_text(index, text):
    text_data = {
        "insertText":
            {
                "text": text,
                "location":
                    {
                        "index": index
                    }
            }
    }

    return text_data


def insert_table_data(file_id, values):
    documentId = file_id
    resource = {
        "oauth2": credentials,
        "documentId": documentId,
        "rows": len(values),
        "columns": len(values[0]),
        "append": True,
        "values": values,
    }
    gdoctableapp.CreateTable(resource)
    resource = {
        "oauth2": credentials,
        "documentId": documentId,
    }
    res = gdoctableapp.GetTables(resource)
    obj = {"color": {"color": {}}, "dashStyle": "SOLID", "width": {"magnitude": 0, "unit": "PT"}}
    data = {
        "updateTableCellStyle": {
            "tableCellStyle": {
                "borderBottom": obj,
                "borderTop": obj,
                "borderLeft": obj,
                "borderRight": obj,
            },
            "tableStartLocation": {
                "index": res['tables'][-1]['tablePosition']['startIndex']
            },
            "fields": "borderBottom,borderTop,borderLeft,borderRight"
        }
    }
    # docs.documents().batchUpdate(documentId=documentId, body={'requests': requests}).execute()
    return data


def download_as_docx(file_id):
    results = drive.files().get(fileId=file_id, fields="id, name, mimeType, createdTime").execute()
    docMimeType = results['mimeType']
    mimeTypeMatchup = {
        "application/vnd.google-apps.document": {
            "exportType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docExt": "docx"
        }
    }
    exportMimeType = mimeTypeMatchup[docMimeType]['exportType']
    # docExt = mimeTypeMatchup[docMimeType]['docExt']
    docName = results['name']
    request = drive.files().export_media(fileId=file_id,
                                         mimeType=exportMimeType)  # Export formats : https://developers.google.com/drive/api/v3/ref-export-formats
    # fh = io.FileIO(docName + "." + docExt, mode='w')
    fh = io.FileIO(docName, mode='w')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))


def download_as_pdf(file_id, file_name):
    request = drive.files().export_media(fileId=file_id,
                                         mimeType='application/pdf')
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
    fh.seek(0)
    filename = file_name.split('.docx')[0] + '.pdf'
    with open(filename, 'wb') as fx:
        fx.write(fh.getvalue())


def delete_gdrive_file(file_id):
    """Deleted file on Google Drive
    :param file_id: ID of Google Drive file
    """
    response = drive.files().delete(fileId=file_id).execute()
    print(response)


if __name__ == '__main__':
    file_name = 'Data.docx'
    file_id = create_file(file_name)
    insert_data(file_id)
    download_as_docx(file_id)
    download_as_pdf(file_id, file_name)
    delete_gdrive_file(file_id)

错误:

googleapiclient.errors.HttpError:https://docs.googleapis.com/v1/documents/1Qsf3CRKiIS9Ayjws1GEhg5oGiZbrTBAObMsifxdfre:b atchUpdate?alt=json时返回“无效的请求3.updateTableCellStyle:提供的表开始位置无效”。详细信息:“无效请求3.up dateTableCellStyle:提供的表开始位置无效.”>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-24 12:24:59

当我在表被gdoctableapp追加后看到您的脚本时,文本和图像将从文档的顶部插入,并运行删除边框的请求。这样,附加表的索引就不同了。这样的错误就会发生。我以为这就是你出问题的原因。为了消除这个问题,下面的修改如何?

发自:

代码语言:javascript
复制
def insert_data(file_id):
    requests = []
    requests.append(insert_image())
    requests.append(insert_text(2, '\n'))
    requests.append(insert_text(3, 'text'))
    values = [['Name of the Client/Organization', 'XYZ'], ['Industry', 'Software']]
    requests.append(insert_table_data(file_id, values))

    result = docs.documents().batchUpdate(documentId=file_id, body={'requests': requests}).execute()

至:

代码语言:javascript
复制
def insert_data(file_id):
    requests = []
    values = [['Name of the Client/Organization', 'XYZ'], ['Industry', 'Software']]
    requests.append(insert_table_data(file_id, values))
    requests.append(insert_image())
    requests.append(insert_text(2, '\n'))
    requests.append(insert_text(3, 'text'))

    result = docs.documents().batchUpdate(documentId=file_id, body={'requests': requests}).execute()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70471393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档