Skip to content

Commit

Permalink
Update convertor.py
Browse files Browse the repository at this point in the history
  • Loading branch information
uibm committed Dec 16, 2024
1 parent 0698df3 commit 8e01ff9
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/boxtodocx/convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,35 @@ def convert(
# Set up all paths
docx_path, assets_dir, html_path = self._setup_paths(filepath, generate_html)
if not generate_html:
temp_dir = assets_dir # Store temp_dir for cleanup
temp_dir = assets_dir

# Initialize handlers
# Process file
with open(str(filepath), 'r', encoding='utf-8') as f: # Convert to str here
data = json.load(f)

# Initialize HTML handler and convert
html_handler = HTMLHandler(str(assets_dir))
if api_token:
html_handler.set_api_token(api_token)

# Convert to HTML first
html_content, image_paths = html_handler.convert_file(
str(filepath),
html_content, image_paths = html_handler.convert_to_html(
data['doc']['content'],
credentials,
api_token
)

# Write HTML file
with open(html_path, 'w', encoding='utf-8') as f:
f.write(html_content)
html_path.parent.mkdir(parents=True, exist_ok=True)
with open(str(html_path), 'w', encoding='utf-8') as f: # Convert to str here
f.write(str(html_content)) # Ensure html_content is string

# Convert to DOCX
docx_handler = DOCXHandler()
docx_handler.convert_html_to_docx(
html_content,
docx_path,
assets_dir
str(html_content), # Ensure html_content is string
str(docx_path), # Convert to str here
str(assets_dir) # Convert to str here
)

logger.info(f"Successfully converted {filepath.name}")
Expand All @@ -86,11 +91,10 @@ def convert(
logger.error(f"Conversion failed for {filepath}: {str(e)}")
raise
finally:
# Cleanup temporary directory if used
if temp_dir and not generate_html:
import shutil
try:
shutil.rmtree(temp_dir)
shutil.rmtree(str(temp_dir)) # Convert to str here
logger.debug(f"Cleaned up temporary directory: {temp_dir}")
except Exception as e:
logger.warning(f"Failed to cleanup temporary directory: {e}")
Expand Down

0 comments on commit 8e01ff9

Please sign in to comment.