Skip to content

Commit c6fabcb

Browse files
Update getPageInfoAndSavePng.py
1 parent 1900cbe commit c6fabcb

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

Uses-Cases/Pages/get/getPageInfoAndSavePng.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,50 +34,36 @@ def _init_api(self, credentials_file: Path):
3434
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3535
logging.error(f"init_api(): Failed to load credentials: {e}")
3636

37-
def _ensure_api_initialized(self):
38-
""" Check if the API is initialized before making API calls. """
39-
if not self.pdf_api:
40-
logging.error("ensure_api_initialized(): PDF API is not initialized. Operation aborted.")
41-
return False
42-
return True
43-
4437
def upload_document(self):
4538
""" Upload a PDF document to the Aspose Cloud server. """
46-
if not self._ensure_api_initialized():
47-
return
48-
49-
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
50-
try:
51-
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
52-
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
53-
except Exception as e:
54-
logging.error(f"upload_document(): Failed to upload file: {e}")
39+
if self.pdf_api:
40+
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
41+
try:
42+
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
43+
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
44+
except Exception as e:
45+
logging.error(f"upload_document(): Failed to upload file: {e}")
5546

5647
def get_page_info(self):
5748
""" Get page information of the PDF document. """
58-
if not self._ensure_api_initialized():
59-
return
60-
61-
result_pages: DocumentPagesResponse = self.pdf_api.get_page(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
49+
if self.pdf_api:
50+
result_pages: DocumentPagesResponse = self.pdf_api.get_page(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
6251

63-
if result_pages.code == 200:
64-
logging.info(f"Page #{Config.PAGE_NUMBER} information: {result_pages.page}")
65-
else:
66-
logging.error(f"Failed to get the page #{Config.PAGE_NUMBER}.")
52+
if result_pages.code == 200:
53+
logging.info(f"Page #{Config.PAGE_NUMBER} information: {result_pages.page}")
54+
else:
55+
logging.error(f"Failed to get the page #{Config.PAGE_NUMBER}.")
6756

6857
def get_page_as_png(self):
6958
""" Get page information of the PDF document. """
70-
if not self._ensure_api_initialized():
71-
return
72-
73-
try:
74-
result_pages = self.pdf_api.get_page_convert_to_png(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
75-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
76-
shutil.move(result_pages, str(local_path))
77-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
78-
except Exception as e:
79-
logging.error(f"download_result(): Failed to download file: {e}")
80-
59+
if self.pdf_api:
60+
try:
61+
result_pages = self.pdf_api.get_page_convert_to_png(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
62+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
63+
shutil.move(result_pages, str(local_path))
64+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
65+
except Exception as e:
66+
logging.error(f"download_result(): Failed to download file: {e}")
8167

8268
if __name__ == "__main__":
8369
pdf_pages = PdfPages()

0 commit comments

Comments
 (0)