1

I'm trying to export a .doc file as html from Google Drive into. Here is my code. I don't see anything in the documentation on how to download a doc as html. But here is my code so far from the examples. I'm not sure what the docsfile is referring to.

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)


test='https://docs.google.com/document/d/116rpW5DxfVDFTfJeCh3pFl9K8gtuZV5gX035631SKjm8/edit'
docsfile.GetContentFile(test, mimetype='text/html')
trinchet
  • 6,753
  • 4
  • 37
  • 60
jason
  • 3,811
  • 18
  • 92
  • 147
  • 1
    Is this all of your code? I'd be surprised if this ran without specifying what `docsfile` is. – Jenner Felton May 30 '16 at 08:38
  • this is what is posted as an example. it doesn't run obviously. here is the link https://github.com/googledrive/PyDrive – jason May 30 '16 at 10:34

1 Answers1

2

First of all docsfile is the file you are intending to export, in your case the .doc file that already are in Google Drive.

docsfile = drive.CreateFile({'id': <ID of your file>)

You can see more about how to download files here. Here the full documentation http://pythonhosted.org/PyDrive/

Alternatively, you can export your files as html using the python client Google provides, directly:

    response = service.files().export(
        fileId=fileid, mimeType='text/html'
    ).execute()
    if response:
        with open(<full_path_of_your_destination_html_file>, 'wb') as fh:
            fh.write(response)
    else:
        <handle error here>

where service is something like:

    store = oauth2client.file.Storage(<path_to_your_credentials>)
    credentials = store.get()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http) 

see a full example on how to use the google client here.

Something to have in mind, your file in Google Drive must be a Google Doc (application/vnd.google-apps.document), not a doc file (application/msword), so you should be sure that the file was uploaded as a valid Google Doc.

trinchet
  • 6,753
  • 4
  • 37
  • 60
  • This does not look like valid Python at the place it is being used: `mimeType=mimetype='text/html'`. In Python, attribution is a statement instead of an expression, and you can't use statements as arguments to function calls. – Paulo Scardine May 30 '16 at 12:25
  • It was a typo @PauloScardine. Updated the response. Thanks – trinchet May 30 '16 at 12:32