Create and manage documents

Stay organized with collections Save and categorize content based on your preferences.

This page describes how to perform certain high-level tasks involving documents, such as:

  • Create a document
  • Copy an existing document

The following paragraphs describe these tasks in detail.

Create a blank document

To create a document, use the create method on the documents collection, as shown in the following example.

This example creates a blank document with a specified title.

Java

private static void createDoc(Docs service) throws IOException {
    Document doc = new Document()
            .setTitle("My Document");
    doc = service.documents().create(doc)
            .execute();
    System.out.println("Created document with title: " + doc.getTitle());
}

PHP

$title = 'My Document';
$document = new Google_Service_Docs_Document(array(
    'title' => $title
));

$document = $service->documents->create($document);
printf("Created document with title: %s\n", $document->title);

Python

title = 'My Document'
body = {
    'title': title
}
doc = service.documents() \
    .create(body=body).execute()
print('Created document with title: {0}'.format(
    doc.get('title')))

Work with Google Drive folders

There’s no option to create a document directly within a specified Drive folder using the Docs API. By default, the created document is saved to the user’s root folder on Drive.

However, there are 2 alternatives to saving a file to a Drive folder:

  • After the document is created, move it to a specific folder using the files.update method of the Drive API. For more information on moving files, refer to Move files between folders.
  • Add a blank document to a folder using the files.create method of the Drive API, specifying application/vnd.google-apps.document as the mimeType. For more information on creating files, refer to Create a file in a folder.

For either alternative, you'll need to add the appropriate Drive API scopes to authorize the call.

To move or create a file within a shared drive folder, refer to Implement shared drive support.

Copy an existing document

To copy a document, use Drive API's files().copy method.

The following examples copy an existing document. You can find the ID to use for the Drive API call in the URL of the document.

https://docs.google.com/document/d/document_id/edit

Java

String copyTitle = "Copy Title";
File copyMetadata = new File().setName(copyTitle);
File documentCopyFile =
        driveService.files().copy(documentId, copyMetadata).execute();
String documentCopyId = documentCopyFile.getId();

Node.js

var copyTitle = "Copy Title";
let request = {
  name: copyTitle,
};
this.driveService.files.copy({
  fileId: documentId,
  resource: request,
}, (err, driveResponse) => {
  let documentCopyId = driveResponse.id;
});

PHP

<?php
$copyTitle = 'Copy Title';
$copy = new Google_Service_Drive_DriveFile(array(
    'name' => $copyTitle
));
$driveResponse = $driveService->files->copy($documentId, $copy);
$documentCopyId = $driveResponse->id;

Python

copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = drive_service.files().copy(
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')

Note that you need to use an appropriate Drive API scope to authorize the call.