Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed one test and used an env var for the google cloud bucket #241

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Choose any of the following input readers:
- tesseract ``invoice2data --input-reader tesseract invoice.pdf``
- pdf miner ``invoice2data --input-reader pdfminer invoice.pdf``
- tesseract4 ``invoice2data --input-reader tesseract4 invoice.pdf``
- gvision ``invoice2data --input-reader gvision invoice.pdf`` (needs ``GOOGLE_APPLICATION_CREDENTIALS`` env var)
- gvision ``invoice2data --input-reader gvision invoice.pdf`` (needs ``GOOGLE_APPLICATION_CREDENTIALS`` and a Google Cloud Bucket name. The bucket name can be set as an argument to the function ``to_text`` or as an Environment variable named ``GOOGLE_CLOUD_BUCKET_NAME`` )

Choose any of the following output formats:

Expand Down
10 changes: 9 additions & 1 deletion src/invoice2data/input/gvision.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
def to_text(path, bucket_name='cloud-vision-84893', language='fr'):
def to_text(path, bucket_name=None, language='fr'):
"""Sends PDF files to Google Cloud Vision for OCR.

Before using invoice2data, make sure you have the auth json path set as
Expand Down Expand Up @@ -28,6 +28,14 @@ def to_text(path, bucket_name='cloud-vision-84893', language='fr'):
# Supported mime_types are: 'application/pdf' and 'image/tiff'
mime_type = 'application/pdf'

if bucket_name is None:
bucket_name = os.getenv('GOOGLE_CLOUD_BUCKET_NAME', None)

if bucket_name is None:
raise EnvironmentError(
'No Google Cloud Bucket name set.\n Set it as an input variable or as an environment variable named GOOGLE_CLOUD_BUCKET_NAME'
)

path_dir, filename = os.path.split(path)
result_blob_basename = filename.replace('.pdf', '').replace('.PDF', '')
result_blob_name = result_blob_basename + '/output-1-to-1.json'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_copy(self):
i += 1

shutil.rmtree('tests/copy_test/', ignore_errors=True)
self.assertEqual(i, len(get_sample_files('.json')))
self.assertEqual(i, len(get_sample_files('.pdf')))
'''
if i != len(self._get_test_file_json_path()):
print(i)
Expand Down