Skip to content

Commit 581a21e

Browse files
Update appendTextHeader.py
1 parent 68faf0f commit 581a21e

File tree

1 file changed

+49
-64
lines changed

1 file changed

+49
-64
lines changed

Uses-Cases/HeaderFooter/appendTextHeader.py

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,83 +34,68 @@ 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 download_result(self):
5748
""" Download the processed PDF document from the Aspose Cloud server. """
58-
if not self._ensure_api_initialized():
59-
return
60-
61-
try:
62-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
63-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
64-
shutil.move(temp_file, str(local_path))
65-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
66-
except Exception as e:
67-
logging.error(f"download_result(): Failed to download file: {e}")
49+
if self.pdf_api:
50+
try:
51+
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
52+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
53+
shutil.move(temp_file, str(local_path))
54+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
55+
except Exception as e:
56+
logging.error(f"download_result(): Failed to download file: {e}")
6857

6958
def append_text_header(self):
7059
"""Append a new text header to the PDF document."""
71-
if not self._ensure_api_initialized():
72-
return
73-
74-
new_header = TextHeader(
75-
background = True,
76-
horizontal_alignment = HorizontalAlignment.CENTER,
77-
text_alignment = TextHorizontalAlignment.CENTER,
78-
value = Config.HEADER_VALUE
79-
)
80-
81-
try:
82-
response = self.pdf_api.post_document_text_header(
83-
Config.PDF_DOCUMENT_NAME, new_header
60+
if self.pdf_api:
61+
new_header = TextHeader(
62+
background = True,
63+
horizontal_alignment = HorizontalAlignment.CENTER,
64+
text_alignment = TextHorizontalAlignment.CENTER,
65+
value = Config.HEADER_VALUE
8466
)
85-
if response.code == 200:
86-
logging.info(f"append_text_header(): Header '{new_header.value}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
87-
else:
88-
logging.error(f"append_text_header(): Failed to add header '{new_header.value}' to the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
89-
except Exception as e:
90-
logging.error(f"append_text_header(): Error while adding header: {e}")
67+
68+
try:
69+
response = self.pdf_api.post_document_text_header(
70+
Config.PDF_DOCUMENT_NAME, new_header
71+
)
72+
if response.code == 200:
73+
logging.info(f"append_text_header(): Header '{new_header.value}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
74+
else:
75+
logging.error(f"append_text_header(): Failed to add header '{new_header.value}' to the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
76+
except Exception as e:
77+
logging.error(f"append_text_header(): Error while adding header: {e}")
9178

9279
def append_text_header_page(self):
9380
"""Append a new text footer to the page on PDF document."""
94-
if not self._ensure_api_initialized():
95-
return
96-
97-
new_header = TextHeader(
98-
background = True,
99-
horizontal_alignment = HorizontalAlignment.LEFT,
100-
text_alignment = TextHorizontalAlignment.CENTER,
101-
value = Config.HEADER_VALUE
102-
)
103-
104-
try:
105-
response = self.pdf_api.post_document_text_header(
106-
Config.PDF_DOCUMENT_NAME, new_header, start_page_number=Config.PAGE_NUMBER, end_page_number=Config.PAGE_NUMBER
81+
if self.pdf_api:
82+
new_header = TextHeader(
83+
background = True,
84+
horizontal_alignment = HorizontalAlignment.LEFT,
85+
text_alignment = TextHorizontalAlignment.CENTER,
86+
value = Config.HEADER_VALUE
10787
)
108-
if response.code == 200:
109-
logging.info(f"append_text_header_page(): Header '{new_header.value}' added to the page #{Config.PAGE_NUMBER}.")
110-
else:
111-
logging.error(f"append_text_header_page(): Failed to add header '{new_header.value}' to the document #{Config.PAGE_NUMBER}. Response code: {response.code}")
112-
except Exception as e:
113-
logging.error(f"append_text_header_page(): Error while adding header: {e}")
88+
89+
try:
90+
response = self.pdf_api.post_document_text_header(
91+
Config.PDF_DOCUMENT_NAME, new_header, start_page_number=Config.PAGE_NUMBER, end_page_number=Config.PAGE_NUMBER
92+
)
93+
if response.code == 200:
94+
logging.info(f"append_text_header_page(): Header '{new_header.value}' added to the page #{Config.PAGE_NUMBER}.")
95+
else:
96+
logging.error(f"append_text_header_page(): Failed to add header '{new_header.value}' to the document #{Config.PAGE_NUMBER}. Response code: {response.code}")
97+
except Exception as e:
98+
logging.error(f"append_text_header_page(): Error while adding header: {e}")
11499

115100

116101
if __name__ == "__main__":

0 commit comments

Comments
 (0)