Skip to content

Commit 94a71a3

Browse files
authored
Merge pull request #90 from aspose-pdf-cloud/Dmitriy-Xawstov-patch-2
Update removeStamps.py
2 parents 5685245 + 888d561 commit 94a71a3

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

Uses-Cases/Stamps/remove/removeStamps.py

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

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

5748
def download_result(self):
5849
""" Download the processed PDF document from the Aspose Cloud server. """
59-
if not self._ensure_api_initialized():
60-
return
61-
62-
try:
63-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
64-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
65-
shutil.move(temp_file, str(local_path))
66-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
67-
except Exception as e:
68-
logging.error(f"download_result(): Failed to download file: {e}")
50+
if self.pdf_api:
51+
try:
52+
file_path = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
53+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
54+
shutil.move(file_path, str(local_path))
55+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
56+
except Exception as e:
57+
logging.error(f"download_result(): Failed to download file: {e}")
6958

7059
def delete_page_stamps(self):
7160
""" Remove stamp in a specific page of the PDF document. """
72-
73-
response: AsposeResponse = self.pdf_api.delete_page_stamps(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
74-
75-
if response.code == 200 :
76-
logging.info(f"Stamps on page #{Config.PAGE_NUMBER} was deleted for the document '{Config.PDF_DOCUMENT_NAME}'.")
77-
else:
78-
logging.error(f"Failed to remove stamps on page #{Config.PAGE_NUMBER} for the document '{Config.PDF_DOCUMENT_NAME}'.")
61+
if self.pdf_api:
62+
response: AsposeResponse = self.pdf_api.delete_page_stamps(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
63+
64+
if response.code == 200 :
65+
logging.info(f"Stamps on page #{Config.PAGE_NUMBER} was deleted for the document '{Config.PDF_DOCUMENT_NAME}'.")
66+
else:
67+
logging.error(f"Failed to remove stamps on page #{Config.PAGE_NUMBER} for the document '{Config.PDF_DOCUMENT_NAME}'.")
7968

8069
def delete_stamp_by_id(self):
8170
""" Remove stamp by Id in the PDF document. """
82-
83-
response: AsposeResponse = self.pdf_api.delete_stamp(Config.PDF_DOCUMENT_NAME, Config.STAMP_ID)
84-
85-
if response.code == 200 :
86-
logging.info(f"Stamps with Id '{Config.STAMP_ID}' was deleted for the document '{Config.PDF_DOCUMENT_NAME}'.")
87-
else:
88-
logging.error(f"Failed to remove stamp with Id '{Config.STAMP_ID}' for the document '{Config.PDF_DOCUMENT_NAME}'.")
71+
if self.pdf_api:
72+
try:
73+
response: AsposeResponse = self.pdf_api.delete_stamp(Config.PDF_DOCUMENT_NAME, Config.STAMP_ID)
74+
75+
if response.code == 200 :
76+
logging.info(f"Stamps with Id '{Config.STAMP_ID}' was deleted for the document '{Config.PDF_DOCUMENT_NAME}'.")
77+
else:
78+
logging.error(f"Failed to remove stamp with Id '{Config.STAMP_ID}' for the document '{Config.PDF_DOCUMENT_NAME}'.")
79+
except Exception as e:
80+
logging.error(f"delete_stamp_by_id(): Failed to download file: {e}")
8981

9082
if __name__ == "__main__":
9183
pdf_stamps = PdfStamps()

0 commit comments

Comments
 (0)