diff --git a/src/boxtodocx/convertor.py b/src/boxtodocx/convertor.py index 4e90e07..e1c3c58 100644 --- a/src/boxtodocx/convertor.py +++ b/src/boxtodocx/convertor.py @@ -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}") @@ -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}")