Skip to content

Commit 8893c21

Browse files
Update removePage.py
1 parent c2b5ed0 commit 8893c21

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

Uses-Cases/Pages/remove/removePage.py

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

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

5546
def download_result(self):
5647
""" Download the processed PDF document from the Aspose Cloud server. """
57-
if not self._ensure_api_initialized():
58-
return
59-
60-
try:
61-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
62-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
63-
shutil.move(temp_file, 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}")
48+
if self.pdf_api:
49+
try:
50+
file_path = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
51+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
52+
shutil.move(file_path, str(local_path))
53+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
54+
except Exception as e:
55+
logging.error(f"download_result(): Failed to download file: {e}")
6756

6857
def delete_page(self):
6958
""" Deletes a specific page from a PDF document. """
70-
if not self._ensure_api_initialized():
71-
return
72-
73-
result = self.pdf_api.delete_page(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
74-
if result.code == 200:
75-
logging.info(f"Page #{Config.PAGE_NUMBER} deleted.")
76-
else:
77-
logging.error(f"Failed to delete page #{Config.PAGE_NUMBER}.")
78-
59+
if self.pdf_api:
60+
result = self.pdf_api.delete_page(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
61+
if result.code == 200:
62+
logging.info(f"Page #{Config.PAGE_NUMBER} deleted.")
63+
else:
64+
logging.error(f"Failed to delete page #{Config.PAGE_NUMBER}.")
7965

8066
if __name__ == "__main__":
8167
pdf_pages = PdfPages()

0 commit comments

Comments
 (0)