English 中文(简体)
• 如何在谷歌病毒药品中添加已加满的超文本模板
原标题:How insert a rendered HTML template into Google Docs using Python

我面临的挑战是,将一个模板(即一个超文本模板)列入一个谷歌文件,在方案上使用on。 我知道,在谷歌、文件编辑或谷歌式文件APIC中,没有解决我的问题的本土/传统特征,但我尝试了几条trick,以接触我的喷气。 在此,我们重新忽略我们应插入的“哪里”文件,仅仅成功插入就足够了。

我的做法是:

  1. Upload a HTML file in Google Drive as application/vnd.google-apps.document, because Google Docs convert the HTML to Docs automatically. (Not perfect, but works)
  2. Get the file content using Google Docs API get(), which is the Google Docs JSON format.
  3. Update the new content on target file using Google Docs batchUpdate().
def insert_template_to_file(target_file_id, content):
    media = MediaIoBaseUpload(BytesIO(content.encode( utf-8 )), mimetype= text/html , resumable=True)
    body = {
         name :  document_test_html ,
         mimeType :  application/vnd.google-apps.document ,
         parents : [DOC_FOLDER_ID]
    }

    try:
        # Create HTML as docs because it automatically convert html to docs
        content_file = driver_service.files().create(body=body, media_body=media).execute()
        content_file_id = content_file.get( id )

        # Collect html content from Google Docs after created
        doc = docs_service.documents().get(documentId=content_file_id, fields= body ).execute()
        request_content = doc.get( body ).get( content )

        # Insert the content from html to target file
        result = docs_service.documents().batchUpdate(documentId=target_file_id, body={ requests : request_content}).execute()
        print(result)

        # Delete html docs
        driver_service.files().delete(fileId=content_file_id).execute()
        print("Content inserted successfuly")
    except HttpError as error:
        # Delete html docs even if failed
        driver_service.files().delete(fileId=content_file_id).execute()
        print(f"An error occurred: {error}")

www.un.org/Depts/DGACM/index_spanish.htm 问题是:。 我从第2步中收集的内容与更新所需内容相符。 我试图将内容从第2步调整到第3步,但还没有成功。

www.un.org/Depts/DGACM/index_spanish.htm 目标解决办法: 采用超文本,insert,将超文本输入谷歌文件的目标档案。 这一目标与目标文件的现有内容而不是超标。

我的做法是否有意义? 你们是否还有其他想法来达到我的目标?

问题回答

我认为你的目标如下。

  • You want to append HTML data to a Google Document by rendering the HTML.
  • You want to achieve this using googleapis for Python.

不幸的是,在目前阶段,似乎无法直接将“Method: documents.get”检索的JSON物体作为“Method: documents.batchUpdate”的要求机构。

但是,如果你想对现有的谷歌文件表示超声,我就认为,只能使用APIC。 如果在样本说明中反映这一点,那么下面的样本说明如何?

Sample script:

def insert_template_to_file(target_file_id, content):
    request = drive_service.files().export(fileId=target_file_id, mimeType="text/html")
    file = BytesIO()
    downloader = MediaIoBaseDownload(file, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%" % int(status.progress() * 100))
    file.seek(0)
    current_html = file.read()

    media = MediaIoBaseUpload(BytesIO(current_html + content.encode( utf-8 )), mimetype= text/html , resumable=True)
    body = { mimeType :  application/vnd.google-apps.document }

    try:
        result = drive_service.files().update(fileId=target_file_id, body=body, media_body=media).execute()
        print(result)
        print("Content inserted successfuly")
    except HttpError as error:
        print(f"An error occurred: {error}")
  • In this modified script, HTML data is retrieved from the existing Google Document, and the new HTML is appended to the retrieved HTML. And, the Google Document is updated by updated HTML. In your situation, it seems that the original data is HTML. So, I thought that this method might be able to be used.

Note:

  • This script overwrites the Google Document of target_file_id. So, when you test this script, I would like to recommend using a sample Google Document.

References:





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签