Skip to content

Commit 68faf0f

Browse files
Update appendTextFooter.py
1 parent f7cb221 commit 68faf0f

File tree

1 file changed

+66
-83
lines changed

1 file changed

+66
-83
lines changed

Uses-Cases/HeaderFooter/appendTextFooter.py

Lines changed: 66 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -35,106 +35,89 @@ 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+
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
53+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
54+
shutil.move(temp_file, 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 append_text_footer(self):
7160
"""Append a new text footer to the PDF document."""
72-
if not self._ensure_api_initialized():
73-
return
74-
75-
new_footer = TextFooter(
76-
background = True,
77-
horizontal_alignment = HorizontalAlignment.CENTER,
78-
text_alignment = TextHorizontalAlignment.CENTER,
79-
value = Config.FOOTER_VALUE
80-
)
81-
82-
try:
83-
response = self.pdf_api.post_document_text_footer(
84-
Config.PDF_DOCUMENT_NAME, new_footer
61+
if self.pdf_api:
62+
new_footer = TextFooter(
63+
background = True,
64+
horizontal_alignment = HorizontalAlignment.CENTER,
65+
text_alignment = TextHorizontalAlignment.CENTER,
66+
value = Config.FOOTER_VALUE
8567
)
86-
if response.code == 200:
87-
logging.info(f"append_text_footer(): Footer '{new_footer.value}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
88-
else:
89-
logging.error(f"append_text_footer(): Failed to add footer '{new_footer.value}' to the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
90-
except Exception as e:
91-
logging.error(f"append_text_footer(): Error while adding footer: {e}")
68+
69+
try:
70+
response = self.pdf_api.post_document_text_footer(
71+
Config.PDF_DOCUMENT_NAME, new_footer
72+
)
73+
if response.code == 200:
74+
logging.info(f"append_text_footer(): Footer '{new_footer.value}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
75+
else:
76+
logging.error(f"append_text_footer(): Failed to add footer '{new_footer.value}' to the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
77+
except Exception as e:
78+
logging.error(f"append_text_footer(): Error while adding footer: {e}")
9279

9380
def append_text_footer(self):
9481
"""Append a new text footer to the PDF document."""
95-
if not self._ensure_api_initialized():
96-
return
97-
98-
new_footer = TextFooter(
99-
background = True,
100-
horizontal_alignment = HorizontalAlignment.CENTER,
101-
text_alignment = TextHorizontalAlignment.CENTER,
102-
value = Config.FOOTER_VALUE
103-
)
104-
105-
try:
106-
response = self.pdf_api.post_document_text_footer(
107-
Config.PDF_DOCUMENT_NAME, new_footer
82+
if self.pdf_api:
83+
new_footer = TextFooter(
84+
background = True,
85+
horizontal_alignment = HorizontalAlignment.CENTER,
86+
text_alignment = TextHorizontalAlignment.CENTER,
87+
value = Config.FOOTER_VALUE
10888
)
109-
if response.code == 200:
110-
logging.info(f"append_text_footer(): Footer '{new_footer.value}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
111-
else:
112-
logging.error(f"append_text_footer(): Failed to add footer '{new_footer.value}' to the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
113-
except Exception as e:
114-
logging.error(f"append_text_footer(): Error while adding footer: {e}")
89+
90+
try:
91+
response = self.pdf_api.post_document_text_footer(
92+
Config.PDF_DOCUMENT_NAME, new_footer
93+
)
94+
if response.code == 200:
95+
logging.info(f"append_text_footer(): Footer '{new_footer.value}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
96+
else:
97+
logging.error(f"append_text_footer(): Failed to add footer '{new_footer.value}' to the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
98+
except Exception as e:
99+
logging.error(f"append_text_footer(): Error while adding footer: {e}")
115100

116101
def append_text_footer_page(self):
117102
"""Append a new text footer to the page on PDF document."""
118-
if not self._ensure_api_initialized():
119-
return
120-
121-
new_footer = TextFooter(
122-
background = True,
123-
horizontal_alignment = HorizontalAlignment.RIGHT,
124-
text_alignment = TextHorizontalAlignment.CENTER,
125-
value = Config.FOOTER_VALUE
126-
)
127-
128-
try:
129-
response = self.pdf_api.post_document_text_footer(
130-
Config.PDF_DOCUMENT_NAME, new_footer, start_page_number=Config.PAGE_NUMBER, end_page_number=Config.PAGE_NUMBER
103+
if self.pdf_api:
104+
new_footer = TextFooter(
105+
background = True,
106+
horizontal_alignment = HorizontalAlignment.RIGHT,
107+
text_alignment = TextHorizontalAlignment.CENTER,
108+
value = Config.FOOTER_VALUE
131109
)
132-
if response.code == 200:
133-
logging.info(f"append_text_footer_page(): Footer '{new_footer.value}' added to the page #{Config.PAGE_NUMBER}.")
134-
else:
135-
logging.error(f"append_text_footer_page(): Failed to add footer '{new_footer.value}' to the document #{Config.PAGE_NUMBER}. Response code: {response.code}")
136-
except Exception as e:
137-
logging.error(f"append_text_footer_page(): Error while adding footer: {e}")
110+
111+
try:
112+
response = self.pdf_api.post_document_text_footer(
113+
Config.PDF_DOCUMENT_NAME, new_footer, start_page_number=Config.PAGE_NUMBER, end_page_number=Config.PAGE_NUMBER
114+
)
115+
if response.code == 200:
116+
logging.info(f"append_text_footer_page(): Footer '{new_footer.value}' added to the page #{Config.PAGE_NUMBER}.")
117+
else:
118+
logging.error(f"append_text_footer_page(): Failed to add footer '{new_footer.value}' to the document #{Config.PAGE_NUMBER}. Response code: {response.code}")
119+
except Exception as e:
120+
logging.error(f"append_text_footer_page(): Error while adding footer: {e}")
138121

139122
if __name__ == "__main__":
140123
pdf_header_footer = pdfHederFooter()

0 commit comments

Comments
 (0)