# Rotate PDF

## Demo

* Needs: rotate each pdf file clockwise 90 degree after changing from ps file
* Before: ![](https://i.niupic.com/images/2021/01/07/98n5.png)
* After: ![](https://i.niupic.com/images/2021/01/07/98n6.png)

```python
import os
from PyPDF2 import PdfFileReader, PdfFileWriter
for i in os.listdir(os.getcwd()):
    if 'ps2pdf' in i:
        continue
    if 'ps' in i:
        os.system("pstopdf "+i)
        pdffile = i.split('.ps', 1)[0]+".pdf"
        print (pdffile)
        pdf_writer = PdfFileWriter()
        with open(pdffile, 'rb') as fh:
            pdf_reader = PdfFileReader(fh)
            print(pdf_reader.getNumPages())
            page = pdf_reader.getPage(0).rotateClockwise(90)
            pdf_writer.addPage(page)
            with open("rotate_"+pdffile, 'wb') as f:
                pdf_writer.write(f)
```

Reference: 1. [How to Work With a PDF in Python](https://realpython.com/pdf-python/) 2. [python操作PDF------旋转及排序pdf](https://www.cnblogs.com/nanamiyi/p/13615681.html)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://legacy.cookielau.com/archives/3-python/1-pythontools/5-rotatepdf.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
