-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): separate utils helper functions
- Loading branch information
1 parent
48b7a3e
commit 5ce4854
Showing
4 changed files
with
770 additions
and
734 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Utility functions for file operations. This includes reading from WORD and writing to JSON.""" | ||
|
||
import json | ||
import os | ||
from typing import Dict | ||
|
||
import mammoth | ||
|
||
|
||
############################################ | ||
# Public class: FileUtils | ||
############################################ | ||
class FileUtils: | ||
"""A class that contains utility functions for file operations which include reading and writing files.""" | ||
|
||
############################################ | ||
# Public class function: read_html_from_word_file | ||
############################################ | ||
def read_html_from_word_file(self, file_path: str) -> str: | ||
""" | ||
Reads a Word file in .docx format and returns its content as an HTML string. | ||
Args: | ||
filePath (str): The name of the Word file to be read, without the .docx extension. | ||
Returns: | ||
str: The content of the Word file as an HTML string. | ||
""" | ||
source_file_name = file_path + ".docx" | ||
if not os.path.exists(source_file_name): | ||
raise FileNotFoundError("File not found: " + file_path + ".docx") | ||
|
||
with open(source_file_name, "rb") as source_file: | ||
try: | ||
result = mammoth.convert_to_html(source_file) | ||
return result.value # The generated HTML | ||
except ValueError as error: | ||
raise ValueError('Error converting file: ' + | ||
str(error)) from error | ||
|
||
############################################ | ||
# Public class function: write_json | ||
############################################ | ||
def write_json(self, data: Dict, file_path: str) -> None: | ||
""" | ||
Serializes a data dictionary as a JSON formatted string and writes it to a file. | ||
Args: | ||
data (Dict): The data dictionary to be serialized and written. | ||
file_path (str): The path to the file to be written, without the .json extension. | ||
Returns: | ||
None | ||
""" | ||
# Serializing json | ||
json_object = json.dumps(data, indent=4, ensure_ascii=False).encode( | ||
'utf8').decode('utf8') | ||
|
||
# Writing to target file | ||
target_file_name = file_path + ".json" | ||
try: | ||
with open(target_file_name, "w", encoding='utf-8') as target_file: | ||
target_file.write(json_object) | ||
print(f"Data written to {target_file_name} successfully.") | ||
except IOError: | ||
print(f"Error writing data to {target_file_name}.") |
Oops, something went wrong.