Spaces:
Sleeping
Sleeping
Download file_convert.py from SmartScaling/TutorBot: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/spaces/SmartScaling/TutorBot/resolve/main/file_convert.py
- Command line
-
hf download hf://spaces/SmartScaling/TutorBot/file_convert.py
-
curl -L -o file_convert.py https://huggingface.co/spaces/SmartScaling/TutorBot/resolve/main/file_convert.py
1.79 kB
| import os | |
| import shutil | |
| import sys | |
| # For .docx conversion | |
| try: | |
| from docx2pdf import convert as docx_convert | |
| except ImportError: | |
| print("Error: docx2pdf not installed. Run 'pip install docx2pdf' and try again.") | |
| sys.exit(1) | |
| # For .pptx conversion (requires PowerPoint installed) | |
| try: | |
| import win32com.client | |
| except ImportError: | |
| print("Error: pywin32 not installed. Run 'pip install pywin32' and try again.") | |
| sys.exit(1) | |
| def main(input_dir, output_dir): | |
| if not os.path.isdir(input_dir): | |
| print(f"Error: {input_dir} is not a directory.") | |
| sys.exit(1) | |
| os.makedirs(output_dir, exist_ok=True) | |
| for root, dirs, files in os.walk(input_dir): | |
| rel_dir = os.path.relpath(root, input_dir) | |
| target_subdir = os.path.join(output_dir, rel_dir) | |
| os.makedirs(target_subdir, exist_ok=True) | |
| for file in files: | |
| filepath = os.path.join(root, file) | |
| base, ext = os.path.splitext(file) | |
| output_pdf = os.path.join(target_subdir, base + '.pdf') | |
| if ext.lower() == '.docx': | |
| docx_convert(filepath, output_pdf) | |
| elif ext.lower() == '.pptx': | |
| try: | |
| ppt_app = win32com.client.Dispatch("PowerPoint.Application") | |
| pres = ppt_app.Presentations.Open(os.path.abspath(filepath), WithWindow=False) | |
| pres.SaveAs(output_pdf, 32) # 32 = ppSaveAsPDF | |
| pres.Close() | |
| ppt_app.Quit() | |
| except Exception as e: | |
| print(f"Failed to convert {filepath}: {e}. PowerPoint installed?") | |
| elif ext.lower() == '.pdf': | |
| shutil.copy(filepath, output_pdf) | |
| main("OrigFolder","ConvertedFiles") |