feat: Add cleanup of temporary files in finalize command

- Remove all temporary PNG files after PDF creation
- Remove temporary directory
- Update command feedback messages
This commit is contained in:
Esa Kataja
2024-12-03 20:55:56 +02:00
parent 9bacbb0dcf
commit bad7612027
+9 -3
View File
@@ -120,7 +120,8 @@ def deskew():
@cli.command() @cli.command()
@click.argument('output_pdf', type=click.Path()) @click.argument('output_pdf', type=click.Path())
def finalize(output_pdf): def finalize(output_pdf):
"""Combine processed pages into final PDF.""" """Combine processed pages into final PDF and clean up."""
temp_dir = ensure_temp_dir()
temp_files = get_temp_files() temp_files = get_temp_files()
if not temp_files: if not temp_files:
print("No pages found in temporary directory. Run 'extract' first.") print("No pages found in temporary directory. Run 'extract' first.")
@@ -132,8 +133,13 @@ def finalize(output_pdf):
with open(output_pdf, "wb") as f: with open(output_pdf, "wb") as f:
f.write(img2pdf.convert(temp_files)) f.write(img2pdf.convert(temp_files))
print("PDF created successfully!") # Clean up temporary files
print("Note: Temporary files were kept for further processing if needed.") print("Cleaning up temporary files...")
for file_path in temp_files:
os.remove(file_path)
os.rmdir(temp_dir)
print("PDF created successfully and temporary files removed!")
if __name__ == '__main__': if __name__ == '__main__':
cli() cli()