Python Forum
Cannot find py credentials file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot find py credentials file
#1
I have run this code successfully and now it has stopped. I simply do not understand what is going on.

from adobe.pdfservices.operation.auth.credentials import Credentials
from adobe.pdfservices.operation.exception.exceptions import ServiceApiException, ServiceUsageException, SdkException
from adobe.pdfservices.operation.execution_context import ExecutionContext
from adobe.pdfservices.operation.io.file_ref import FileRef
from adobe.pdfservices.operation.pdfops.extract_pdf_operation import ExtractPDFOperation
from adobe.pdfservices.operation.pdfops.options.extractpdf.extract_pdf_options import ExtractPDFOptions
from adobe.pdfservices.operation.pdfops.options.extractpdf.extract_element_type import ExtractElementType

import os.path
import zipfile
import json

output_zip = "./ExtractTextInfoFromPDF.zip"

if os.path.isfile(output_zip):
    os.remove(output_zip)

input_pdf = "./Adobe Extract API Sample.pdf"

try:

    #Initial setup, create credentials instance.
    credentials = Credentials.service_account_credentials_builder()\
        .from_file("./pdfservices-api-credentials.json") \
        .build()

    #Create an ExecutionContext using credentials and create a new operation instance.
    execution_context = ExecutionContext.create(credentials)
    extract_pdf_operation = ExtractPDFOperation.create_new()

    #Set operation input from a source file.
    source = FileRef.create_from_local_file(input_pdf)
    extract_pdf_operation.set_input(source)

    #Build ExtractPDF options and set them into the operation
    extract_pdf_options: ExtractPDFOptions = ExtractPDFOptions.builder() \
        .with_element_to_extract(ExtractElementType.TEXT) \
        .build()
    extract_pdf_operation.set_options(extract_pdf_options)

    #Execute the operation.
    result: FileRef = extract_pdf_operation.execute(execution_context)

    #Save the result to the specified location.
    result.save_as(output_zip)

    print("Successfully extracted information from PDF. Printing H1 Headers:\n");

    archive = zipfile.ZipFile(output_zip, 'r')
    jsonentry = archive.open('structuredData.json')
    jsondata = jsonentry.read()
    data = json.loads(jsondata)
    for element in data["elements"]:
        if(element["Path"].endswith("/H1")):
            print(element["Text"])

except (ServiceApiException, ServiceUsageException, SdkException):
    logging.exception("Exception encountered while executing operation")
I get this error:

Error:
PS C:\Users\stand\PDFExtractTest> & c:/Users/stand/PDFExtractTest/venv/Scripts/Activate.ps1 & : File C:\Users\stand\PDFExtractTest\venv\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3 + & c:/Users/stand/PDFExtractTest/venv/Scripts/Activate.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess PS C:\Users\stand\PDFExtractTest> & c:/Users/stand/PDFExtractTest/venv/Scripts/python.exe c:/Users/stand/PDFExtractTest/venv/extract.py Traceback (most recent call last): .from_file("./pdfservices-api-credentials.json") \ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\auth\service_account_credentials.py", line 186, in from_file config_json_str = file_utils.read_conf_file_content(credentials_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\internal\util\file_utils.py", line 15, in read_conf_file_content with open(get_file_path(file_path)) as file: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json' PS C:\Users\stand\PDFExtractTest> & c:/Users/stand/PDFExtractTest/venv/Scripts/python.exe c:/Users/stand/PDFExtractTest/venv/extract.py Traceback (most recent call last): .from_file("./pdfservices-api-credentials.json") \ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\auth\service_account_credentials.py", line 186, in from_file config_json_str = file_utils.read_conf_file_content(credentials_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\internal\util\file_utils.py", line 15, in read_conf_file_content with open(get_file_path(file_path)) as file: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json' PS C:\Users\stand\PDFExtractTest> & c:/Users/stand/PDFExtractTest/venv/Scripts/python.exe c:/Users/stand/PDFExtractTest/venv/extract.py Traceback (most recent call last): .from_file("./pdfservices-api-credentials.json") \ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\auth\service_account_credentials.py", line 186, in from_file config_json_str = file_utils.read_conf_file_content(credentials_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\internal\util\file_utils.py", line 15, in read_conf_file_content with open(get_file_path(file_path)) as file: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json' PS C:\Users\stand\PDFExtractTest> & c:/Users/stand/PDFExtractTest/venv/Scripts/python.exe c:/Users/stand/PDFExtractTest/venv/extract.py Traceback (most recent call last): File "c:\Users\stand\PDFExtractTest\venv\extract.py", line 24, in <module> .from_file("./pdfservices-api-credentials.json") \ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\auth\service_account_credentials.py", line 186, in from_file config_json_str = file_utils.read_conf_file_content(credentials_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\internal\util\file_utils.py", line 15, in read_conf_file_content with open(get_file_path(file_path)) as file: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json' PS C:\Users\stand\PDFExtractTest> & c:/Users/stand/PDFExtractTest/venv/Scripts/python.exe c:/Users/stand/PDFExtractTest/venv/extract.py Traceback (most recent call last): File "c:\Users\stand\PDFExtractTest\venv\extract.py", line 24, in <module> .from_file("./pdfservices-api-credentials.json") \ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\auth\service_account_credentials.py", line 186, in from_file config_json_str = file_utils.read_conf_file_content(credentials_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\stand\PDFExtractTest\venv\Lib\site-packages\adobe\pdfservices\operation\internal\util\file_utils.py", line 15, in read_conf_file_content with open(get_file_path(file_path)) as file: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json' PS C:\Users\stand\PDFExtractTest>
Reply
#2
Did you have a look already at the filename mentioned in the error: C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json

That doesn´t look like a valid filename for Windows. I usually try to avoid the "\\" and just use single forward slashes instead when I'm working with paths. Using forward slashes in your Python code stills works on Windows. It is handled by Python. I didn't look at your code yet how you came up with this file path, but you should debug on that.
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply
#3
(Feb-22-2023, 07:40 PM)Jeff900 Wrote: Did you have a look already at the filename mentioned in the error: C:\\Users\\stand\\PDFExtractTest\\./pdfservices-api-credentials.json

That doesn´t look like a valid filename for Windows. I usually try to avoid the "\\" and just use single forward slashes instead when I'm working with paths. Using forward slashes in your Python code stills works on Windows. It is handled by Python. I didn't look at your code yet how you came up with this file path, but you should debug on that.

Yes I agree it looks very odd. As you can see by the code, the target file is pdfservices-api-. credentials.json. In directory C:\Users\stand\PDFExtractTest\venv So why the code puts the initial string in there like that.
Reply
#4
That is an artifact of printing str objects. If your str contains a backslash, it is printed with a double backslash to let you know the "\" is not part of an escape sequence.

I think the confusing part jeff900 refers to is this:
PDFExtractTest\\./pdfservices
To make it easier to see, I'll convert the backshash to '/', which is perfectly fine for a windows path when programming python.
PDFExtractTest/./pdfservices
What is "/./" in your path? That doesn't look right. You cannot have a folder named ".". It also doesn't agree with what you say is the credentials filename.
Reply
#5
(Feb-22-2023, 10:26 PM)deanhystad Wrote: That is an artifact of printing str objects. If your str contains a backslash, it is printed with a double backslash to let you know the "\" is not part of an escape sequence.

I think the confusing part jeff900 refers to is this:
PDFExtractTest\\./pdfservices
To make it easier to see, I'll convert the backshash to '/', which is perfectly fine for a windows path when programming python.
PDFExtractTest/./pdfservices
What is "/./" in your path? That doesn't look right. You cannot have a folder named ".". It also doesn't agree with what you say is the credentials filename.

Yes it looks very odd to me. This is not code I wrote. It is example code in python to use Adobe Extract API found here: https://developer.adobe.com/document-ser...ts/python/
Reply
#6
I have seen the documentation you were referring to. Seems you used the right code. Only thing I would check twice: is that particular file really existing? Or maybe the documentation is referring to kind of "dummy" code and you need to adjust to your situation? Maybe you should check if that file really exists on your machine? Otherwise, point the question to Adobe?
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,585 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  selenium can't find a file in my desk ? SouAmego22 0 747 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Find (each) element from a list in a file tester_V 3 1,225 Nov-15-2022, 08:40 PM
Last Post: tester_V
  what will be the best way to find data in txt file? korenron 2 1,166 Jul-25-2022, 10:03 AM
Last Post: korenron
  find some word in text list file and a bit change to them RolanRoll 3 1,545 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Find and delete above a certain line in text file cubangt 12 3,489 Mar-18-2022, 07:49 PM
Last Post: snippsat
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,634 Jan-13-2022, 05:22 PM
Last Post: ndc85430
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,410 Aug-13-2021, 08:21 PM
Last Post: Winfried
Music XML File - cannot find the tag ateestructural 1 2,121 Apr-06-2021, 08:26 AM
Last Post: ibreeden
  python application and credentials safety concern aster 4 3,519 Mar-06-2021, 06:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020