Feb-22-2023, 06:58 PM
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>
