Search code examples
pythonpdfocrgdatagdata-api

Upload pdf file with gdata docs python v3.0 with ocr


I've got the following implementation for uploading a pdf file to google docs (taken from the gdata API samples):

def UploadResourceSample():
  """Upload a document, and convert to Google Docs."""
  client = CreateClient()
  doc = gdata.docs.data.Resource(type='document', title='My Sample Doc')

  # This is a convenient MS Word doc that we know exists
  path = _GetDataFilePath('test.0.doc')
  print 'Selected file at: %s' % path

  # Create a MediaSource, pointing to the file
  media = gdata.data.MediaSource()
  media.SetFileHandle(path, 'application/msword')

  # Pass the MediaSource when creating the new Resource
  doc = client.CreateResource(doc, media=media)
  print 'Created, and uploaded:', doc.title.text, doc.resource_id.text

Now I would like to perform OCR text recognition on the uploaded file. But I'm not sure how to enable the OCR recognition in gdata docs python API. So my question is: Is there a way to enable OCR recognition using gdata python v3.0 API on a pdf file?


Solution

  • I've managed to get my pdf document OCR'ed using the following code:

    def UploadResourceSample(filename, filepath, fullpath):
      """Upload a document, and convert to Google Docs."""
      client = CreateClient()
      doc = gdata.docs.data.Resource(type='document', title=filename)
    
      path = fullpath
      print 'Selected file at: %s' % path
    
      # Create a MediaSource, pointing to the file
      media = gdata.data.MediaSource()
      media.SetFileHandle(path, 'application/pdf')
    
      # Pass the MediaSource when creating the new Resource
      create_uri = gdata.docs.client.RESOURCE_UPLOAD_URI + '?ocr=true&ocr-language=de'
      doc = client.CreateResource(doc, create_uri=create_uri, media=media)
      print 'Created, and uploaded:', doc.title.text, doc.resource_id.text