Skip to content

Commit 883f005

Browse files
Update appendStamps.py
1 parent 6f16ca2 commit 883f005

File tree

1 file changed

+46
-55
lines changed

1 file changed

+46
-55
lines changed

Uses-Cases/Stamps/add/appendStamps.py

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,75 +38,66 @@ def _init_api(self, credentials_file: Path):
3838
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3939
logging.error(f"init_api(): Failed to load credentials: {e}")
4040

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

6051
def upload_document(self):
6152
""" Upload a PDF document to the Aspose Cloud server. """
6253
self.upload_file(Config.PDF_DOCUMENT_NAME)
6354

6455
def download_result(self):
6556
""" Download the processed PDF document from the Aspose Cloud server. """
66-
if not self._ensure_api_initialized():
67-
return
68-
69-
try:
70-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
71-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
72-
shutil.move(temp_file, str(local_path))
73-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
74-
except Exception as e:
75-
logging.error(f"download_result(): Failed to download file: {e}")
57+
if self.pdf_api:
58+
try:
59+
file_path = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
60+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
61+
shutil.move(file_path, str(local_path))
62+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
63+
except Exception as e:
64+
logging.error(f"download_result(): Failed to download file: {e}")
7665

7766
def add_document_stamps(self):
7867
""" Adds a text stamp to a specific page in a PDF document. """
68+
if self.pdf_api:
69+
text_stamp: Stamp = Stamp(
70+
type = StampType.TEXT,
71+
background = True,
72+
horizontal_alignment = HorizontalAlignment.CENTER,
73+
text_alignment = HorizontalAlignment.CENTER,
74+
value=Config.STAMP_TEXT
75+
)
7976

80-
text_stamp: Stamp = Stamp(
81-
type = StampType.TEXT,
82-
background = True,
83-
horizontal_alignment = HorizontalAlignment.CENTER,
84-
text_alignment = HorizontalAlignment.CENTER,
85-
value=Config.STAMP_TEXT
86-
)
87-
88-
image_stamp: Stamp = Stamp(
89-
type = StampType.IMAGE,
90-
background = True,
91-
horizontal_alignment = HorizontalAlignment.CENTER,
92-
text_alignment = HorizontalAlignment.CENTER,
93-
value = "NEW IMAGE STAMP",
94-
file_name = Config.IMAGE_STAMP_FILE,
95-
y_indent = Config.IMAGE_STAMP_LLY,
96-
width = Config.IMAGE_STAMP_WIDTH,
97-
height = Config.IMAGE_STAMP_HEIGHT
98-
)
99-
100-
responseTextStamp: AsposeResponse = self.pdf_api.post_document_text_stamps(Config.PDF_DOCUMENT_NAME, [ text_stamp ])
101-
responseImageStamp: AsposeResponse = self.pdf_api.post_document_image_stamps(Config.PDF_DOCUMENT_NAME, [ image_stamp ])
77+
image_stamp: Stamp = Stamp(
78+
type = StampType.IMAGE,
79+
background = True,
80+
horizontal_alignment = HorizontalAlignment.CENTER,
81+
text_alignment = HorizontalAlignment.CENTER,
82+
value = "NEW IMAGE STAMP",
83+
file_name = Config.IMAGE_STAMP_FILE,
84+
y_indent = Config.IMAGE_STAMP_LLY,
85+
width = Config.IMAGE_STAMP_WIDTH,
86+
height = Config.IMAGE_STAMP_HEIGHT
87+
)
88+
try:
89+
responseTextStamp: AsposeResponse = self.pdf_api.post_document_text_stamps(Config.PDF_DOCUMENT_NAME, [ text_stamp ])
90+
responseImageStamp: AsposeResponse = self.pdf_api.post_document_image_stamps(Config.PDF_DOCUMENT_NAME, [ image_stamp ])
10291

103-
if responseTextStamp.code == 200 and responseImageStamp.code == 200:
104-
logging.info(f"Text stamp '{Config.STAMP_TEXT}' and image stamp '{Config.IMAGE_STAMP_FILE}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
105-
else:
106-
if responseTextStamp.code != 200:
107-
logging.error(f"Failed to add text stamp '{Config.STAMP_TEXT}' to the document '{Config.PDF_DOCUMENT_NAME}'.")
108-
else:
109-
logging.error(f"Failed to add image stamp '{Config.IMAGE_STAMP_FILE}' to the document '{Config.PDF_DOCUMENT_NAME}'.")
92+
if responseTextStamp.code == 200 and responseImageStamp.code == 200:
93+
logging.info(f"add_document_stamps(): Text stamp '{Config.STAMP_TEXT}' and image stamp '{Config.IMAGE_STAMP_FILE}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
94+
else:
95+
if responseTextStamp.code != 200:
96+
logging.error(f"add_document_stamps(): Failed to add text stamp '{Config.STAMP_TEXT}' to the document '{Config.PDF_DOCUMENT_NAME}'.")
97+
else:
98+
logging.error(f"add_document_stamps(): Failed to add image stamp '{Config.IMAGE_STAMP_FILE}' to the document '{Config.PDF_DOCUMENT_NAME}'.")
99+
except Exception as e:
100+
logging.error(f"add_document_stamps(): Failed to download file: {e}")
110101

111102
if __name__ == "__main__":
112103
pdf_stamps = PdfStamps()

0 commit comments

Comments
 (0)