Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """Rotate all pages of a PDF.""" | |
| import argparse, json, sys | |
| from pathlib import Path | |
| def rotate(input_path, output_path, angle=90): | |
| from PyPDF2 import PdfReader, PdfWriter | |
| reader = PdfReader(input_path) | |
| writer = PdfWriter() | |
| for page in reader.pages: | |
| page.rotate(angle) | |
| writer.add_page(page) | |
| if not output_path: | |
| output_path = str(Path(input_path).with_stem(Path(input_path).stem + '_rotated')) | |
| with open(output_path, 'wb') as f: | |
| writer.write(f) | |
| return output_path | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Rotate PDF pages') | |
| parser.add_argument('--input', required=True) | |
| parser.add_argument('--output', required=True) | |
| parser.add_argument('--angle', type=int, default=90, choices=[90, 180, 270]) | |
| args = parser.parse_args() | |
| try: | |
| result = rotate(args.input, args.output, args.angle) | |
| print(json.dumps({"success": True, "output": result, "message": f"PDF rotated {args.angle}° successfully"})) | |
| except Exception as e: | |
| print(json.dumps({"success": False, "output": "", "message": str(e)})) | |
| sys.exit(1) | |
| if __name__ == '__main__': | |
| main() | |