1

I'm trying to merge data into a Google Docs file with python and the Google Drive API.

I've added this type of syntax to my doc file: {name} and fill this with data like this (python code running on Google App Engine):

self.content = self.drive_service_auth._http.request(self.download_url)

Where self.download_url is the url of the docs file en self.content is the content of this file. I then merge the data like this:

self.content = self.content.replace('{' + name + '}', value)

And then save the file back to Drive:

# metadata from generated document
body = {'title': title,
        'description': desc,
        'mimeType': 'text/html',
        'parents': [{'id': folder_id}]}

# generate new document
media_body = MediaIoBaseUpload(StringIO(self.content.encode('ascii', 'xmlcharrefreplace')), 'text/html', resumable=False)
drive_result = self.drive_service_auth.files().insert(body=body, media_body=media_body, convert=True).execute() 

This works, the data is inserted into the doc file. However, the markup of the original (template) file is different than that of the new file, I don't have controle over it.

I think this is because the doc file is converted to HTML in my python code and when I save it, it's converted back to the original format (XML I think). In these conversions the markup is lost/altered I think.

Is there a better way of accomplishing my goal to merge data in a Google Docs file while still maintaining the option of putting markup in the template file? A way that doesn't destroy the markup of the original file.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
Menno Blok
  • 21
  • 1
  • 5
  • You could try exporting to another format such as OpenOffice Doc (which is an XML format) that may retain the markup better than HTML. It doesn't look like you can get Google Docs' original format - https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents – Adam Jan 16 '16 at 22:49

0 Answers0