Search code examples
pythonpydrive

Pydrive: export doc as html


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')

Solution

  • 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.