使用gdata docs python v3.0上传带OCR的PDF文件

2 投票
1 回答
774 浏览
提问于 2025-04-17 09:19

我有一个实现,可以把PDF文件上传到谷歌文档(这个是从gdata API的示例中拿来的):

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

现在我想对上传的文件进行OCR文字识别。但是我不太确定怎么在gdata docs的Python API中启用OCR识别。所以我的问题是:有没有办法在PDF文件上使用gdata Python v3.0 API启用OCR识别?

1 个回答

3

我用下面的代码成功地对我的PDF文档进行了OCR识别:

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

撰写回答