Search code examples
pythongoogle-app-enginepdf-generationgoogle-docs-api

Retrieving a GoogleDoc via API and generate a PDF replacing with own text


I'm wondering if it's possible to retrieve a simple GoogleDoc (via gdata python client) and render a PDF replacing some custom #[PLACE_HOLDERS] with my own information.


Solution

  • Found what I've looking for. The problem was that I tried to use DocsService (v1 API) instead of DocsClient (v2).

    WARNING: The examples of gdata/OAuth in AppEngine are outdated - http://code.google.com/p/gdata-python-client/source/browse/samples/oauth/oauth_on_appengine/main_hmac.py.

    To get an updated version check this link (It's for Django, but you only have to change the request/response) - http://pamelafox-samplecode.googlecode.com/svn/trunk/spreadsheetsimporter/importer/views.py

    So after changing the Oauth method to a newer version, I managed to get the file contents in HTML,DOC,PDF, TXT, etc. Just change the exportFormat.

    For the future, if anyone needs:

    _client = gdata.docs.client.DocsClient('My-Prety-App-v1')
    # Set your prefered auth method
    # ...
    entry = _client.GetDoc('document:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
    exportFormat = 'html'
    content = _client.GetFileContent(uri=entry.content.src + '&exportFormat=' + exportFormat)
    self.response.out.write(content)