Skip to content

Commit f12c3c7

Browse files
Update wordsCountOnPages.py
1 parent 9812b08 commit f12c3c7

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

Uses-Cases/Pages/wordsCount/wordsCountOnPages.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import shutil
1+
import shutil
22
import json
33
import logging
44
from pathlib import Path
@@ -32,36 +32,25 @@ def _init_api(self, credentials_file: Path):
3232
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3333
logging.error(f"init_api(): Failed to load credentials: {e}")
3434

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

5445
def get_document_words_count_on_pages(self):
5546
""" Retrieves the word count for each page in a PDF document. """
56-
if not self._ensure_api_initialized():
57-
return
58-
59-
response: WordCountResponse = self.pdf_api.get_words_per_page(Config.PDF_DOCUMENT_NAME)
60-
61-
if response.code == 200:
62-
logging.info(response.words_per_page.list)
63-
else:
64-
logging.error("Failed to retrieve word count.")
47+
if self.pdf_api:
48+
response: WordCountResponse = self.pdf_api.get_words_per_page(Config.PDF_DOCUMENT_NAME)
49+
50+
if response.code == 200:
51+
logging.info(response.words_per_page.list)
52+
else:
53+
logging.error("Failed to retrieve word count.")
6554
return
6655

6756
if __name__ == "__main__":

0 commit comments

Comments
 (0)