How do you create a document in Google Docs programmatically?

Alex's answer, while undoubtedly correct, begs the question: "how do I do that via the Google Docs API?"

Here's a way (in Python, 'cause I'm that kind of guy):

import gdata.docs.service
import StringIO

client = gdata.docs.service.DocsService()
client.ClientLogin(username, password,
                   source='Spreadsheet Creation Example')

content = 'COL_A, COL_B, COL_C, COL_D\ndata1, data2, data3, data4'
ms = gdata.MediaSource(file_handle=StringIO.StringIO(content),
                       content_type='text/csv',
                       content_length=len(content))
entry = client.Upload(ms, 'Test Spreadsheet')

This is a small mashup of techniques that I found in http://code.google.com/p/gdata-python-client/source/browse/tests/gdata_tests/docs/service_test.py , which I in turn found via this post from the Google Group for the GData Docs API.

The key insights (for me anyway) were:

  1. realizing that the MediaSource constructor's formal parameter "file_handle" will take any file-like object, and
  2. discovering (as the OP's followup to the Google Group post mentions) that the unit tests are a great source of examples

(I wasn't able to find the Python-specific developer's guide referenced by Alex's doc link -- possibly it's been lost or buried in Google's move of documentation assets from code.google.com to developers.google.com. Alex's link now redirects to the more generic document that shows mostly .NET and Java examples, but only a little Python.)


While the docs call it "uploading", everything boils down to sending an appropriately formatted HTTP POST request, so of course it can actually be a new creation rather than an actual "upload" of an otherwise existing file. (Creation through POST requests is similar to what's normally described as a REST API, though in real REST you'd typically use a PUT request instead of course).

You just need to create a blob of data representing your document in any of the formats listed here -- depending on your programming language, simplest may be text/csv for a spreadsheet and application/rtf for a text-document -- then put in in an appropriately formatted POST data. For example, to make a spreadsheet in the simplest way (no metadata), you could POST something like:

POST /feeds/default/private/full HTTP/1.1
Host: docs.google.com
GData-Version: 3.0
Authorization: <your authorization header here>
Content-Length: 81047
Content-Type: text/csv
Slug: Example Spreadsheet

ColumnA, ColumnB
23, 45

Each specific programming language for which a dedicated API is supplied may offer help with this not-so-hard task; for example, in Python, per the docs, the API recommends using ETags to avoid overwriting changes when multiple clients are simultaneously "uploading" (i.e., creating or updating docs). But preparing the POST directly is always possible, since the almost-REST API is documented as the protocol underlying all language-specific APIs.


As of Feb 4, 2019, Google Docs now has a REST API.

See documentation: https://developers.google.com/docs/api/