How to get direct Google Docs editing URL when uploading files via Python API?

I’m trying to upload a .txt file to Google Drive using Python and need to get a URL that opens directly in Google Docs editor instead of the preview mode.

Here’s my current upload function:

def create_document(self, doc_name, file_path, parent_folder, existing_id=None, content_type='text/plain'):
    upload_media = MediaFileUpload(
        file_path, mimetype=content_type, resumable=True)
    
    try:
        if existing_id is None:
            doc_metadata = {
                "name": doc_name,
                "parents": [parent_folder]
            }
            result = self.service.files().create(
                body=doc_metadata,
                media_body=upload_media, 
                fields='id').execute()
            existing_id = result.get('id')
        else:
            doc_metadata = {"name": doc_name}
            self.service.files().update(
                body=doc_metadata, 
                removeParents='root',
                media_body=upload_media, 
                fileId=existing_id).execute()
        
        drive_url = 'https://drive.google.com/open?id=' + existing_id
        return doc_name, existing_id, drive_url

The problem is this returns a URL that shows the file in Google Drive’s preview mode. Users have to manually click “Open with Google Docs” to start editing.

I want to generate a URL that opens the document directly in Google Docs editing interface. The Google Docs URLs look completely different from Drive URLs. Is there a way to get the direct editing link programmatically after uploading?

Just swap your URL format. Replace https://drive.google.com/open?id= with https://docs.google.com/document/d/{file_id}/edit - opens straight in edit mode. You might need to convert the file first though, not sure if txt files work with docs URLs.

To get direct editing URLs after uploading to Google Drive, convert the file to Google Docs format during upload. Keep your mimetype as ‘text/plain’ but add a ‘convert’ parameter set to true in your files().create() call:

result = self.service.files().create(
    body=doc_metadata,
    media_body=upload_media,
    convert=True,
    fields='id').execute()

This converts your text file to Google Docs format automatically, so you can build the editing link with ‘https://docs.google.com/document/d/’ + existing_id + ‘/edit’. Works great - users can edit directly without hitting the preview page first.

Had this same problem a few months ago with an automated document workflow. The trick is uploading as Google Docs format instead of plain text. Set your mimetype to ‘application/vnd.google-apps.document’ - this creates an actual Google Doc rather than just storing a text file. Once you do that, you can use the standard docs URL. After upload, build your editing link with ‘https://docs.google.com/document/d/’ + file_id + ‘/edit’ using the ID from your create operation. Works great in production and gets rid of that extra click users hate. The converted doc keeps all your original text but gets full Google Docs features.