File size: 1,252 Bytes
80a3675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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()