(file: str, output_dir: str, warning: bool = False, dpi=72, output_type='png')
| 138 | |
| 139 | @tenacity |
| 140 | def ppt_to_images(file: str, output_dir: str, warning: bool = False, dpi=72, output_type='png'): |
| 141 | assert pexists(file), f"File {file} does not exist" |
| 142 | if pexists(output_dir) and warning: |
| 143 | print(f"ppt2images: {output_dir} already exists") |
| 144 | os.makedirs(output_dir, exist_ok=True) |
| 145 | with tempfile.TemporaryDirectory() as temp_dir: |
| 146 | # Create unique user installation directory for LibreOffice to avoid concurrency issues |
| 147 | with tempfile.TemporaryDirectory() as user_install_dir: |
| 148 | command_list = [ |
| 149 | "soffice", |
| 150 | "--headless", |
| 151 | "--norestore", |
| 152 | "--nolockcheck", |
| 153 | f"-env:UserInstallation=file://{user_install_dir}", |
| 154 | "--convert-to", |
| 155 | "pdf", |
| 156 | file, |
| 157 | "--outdir", |
| 158 | temp_dir, |
| 159 | ] |
| 160 | # Set environment to ensure UTF-8 encoding |
| 161 | env = os.environ.copy() |
| 162 | env['LC_ALL'] = 'en_US.UTF-8' |
| 163 | env['LANG'] = 'en_US.UTF-8' |
| 164 | subprocess.run(command_list, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=env) |
| 165 | |
| 166 | for f in os.listdir(temp_dir): |
| 167 | if not f.endswith(".pdf"): |
| 168 | continue |
| 169 | temp_pdf = pjoin(temp_dir, f) |
| 170 | images = convert_from_path(temp_pdf, dpi=72) |
| 171 | for i, img in enumerate(images): |
| 172 | if output_type == 'png': |
| 173 | img.save(pjoin(output_dir, f"poster.png"), 'PNG') |
| 174 | else: |
| 175 | img.save(pjoin(output_dir, f"poster.jpg"), 'JPEG') |
| 176 | return |
| 177 | |
| 178 | raise RuntimeError("No PDF file was created in the temporary directory", file) |
| 179 | |
| 180 | |
| 181 | @tenacity |
no test coverage detected