首页 > 解决方案 > 如何控制 google doc html 上传的格式

问题描述

我正在上传一个嵌入<style>了 p 元素的 html 文档。这似乎控制了顶层的 p 元素,但如果它们在列表中则不会。

这是用于上传文件的 html 的摘录,删除的内容用“:”显示

<!DOCTYPE html>
<head>
<style>
p, li {
    margin-bottom: 0.75em;
}
</style>
</head>
<body>

<h2>Status Report Summary: Board Meeting Demo - 2020-10-12</h2>

:

    <h3>Market Street Mile - Lou King</h3>
    <p>Here's my status for Market Street Mile</p>
    
        <p><b>For discussion:</b></p>
        <ul class="statusreport">
            
                <li><p>Market Street Mile registrations are low</p><p>the market street mile has been suffering</p><ul><li>this is one reason</li><li>this is another reason</li></ul></li>
            
                <li><p>sponsorships are up</p><p>sponsorship details</p></li>
            
        </ul>
    
:

</body>

这在文件中的格式如下

谷歌文档格式化结果

如您所见,p 元素格式适用于“这是我对 Market Street Mile 的状态”,但不是在“Market Street Mile 注册量低”之后。(另请注意 li 元素格式不起作用)。

是否有一些文档说明什么可以和不能用 css 格式化以用于 google doc html 上传,或者描述如何获得所需的格式?

我正在使用google doc api,但我希望在本地上传带有转换的html时也是如此。

20 年 10 月 5 日更新:

作为参考,这里是创建文件的代码。不过,不确定这是否相关。

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

class GoogleAuthService():
    """
    methods to interact with google api using service based credentials (e.g., for server authentication)

    * see https://developers.google.com/identity/protocols/oauth2/service-account

    :param service_account_file: service_account_file.json path
    :param scopes: list of google scopes. see https://developers.google.com/identity/protocols/googlescopes
    :param logdebug: (optional) debug logger function
    :param logerror: (optional) debug logger function
    """

    def __init__(self, service_account_file, scopes, loginfo=None, logdebug=None, logerror=None):
        self.service_account_file = service_account_file
        self.scopes = scopes
        self.loginfo = loginfo
        self.logdebug = logdebug
        self.logerror = logerror
        self.credentials = service_account.Credentials.from_service_account_file(self.service_account_file, scopes=self.scopes)
        self.drive = build('drive', 'v3', credentials=self.credentials)

    def create_file(self, folderid, filename, contents, doctype='html'):
        """
        create file in drive folder

        ..note::
            folderid must be shared read/write with services account email address

        :param folderid: drive id for folder file needs to reside in
        :param filename: name for file on drive
        :param contents: path for file contents (docx formatted)
        :param doctype: 'html' or 'docx', default 'html'
        :return: google drive id for created file
        """
        ## upload (adapted from https://developers.google.com/drive/api/v3/manage-uploads)
        file_metadata = {
            'name': filename,
            # see https://developers.google.com/drive/api/v3/mime-types
            'mimeType': 'application/vnd.google-apps.document',
            # see https://developers.google.com/drive/api/v3/folder
            'parents': [folderid],
        }

        # mimetype depends on doctype
        mimetype = _getmimetype(doctype)

        # create file
        media = MediaFileUpload(
            contents,
            mimetype=mimetype,
            resumable=True
        )
        file = self.drive.files().create(
            body=file_metadata,
            media_body=media,
            fields='id'
        ).execute()
        fileid = file.get('id')
        return fileid

def _getmimetype(doctype):
    """
    determine mimetype for specified doctype
    :param doctype: 'html' or 'docx'
    :return: mimetype
    """
    if doctype not in ['docx', 'html']:
        raise parameterError('_getmimetype(): doctype must be "docx" or "html", found {}'.format(doctype))

    if doctype == 'docx':
        mimetype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    else:  # html
        mimetype = 'text/html'

    return mimetype

标签: google-docsgoogle-docs-api

解决方案


推荐阅读