问题描述
我将一堆文件扫描成pdf,但它们似乎都被旋转了,有没有办法用python旋转页面?
我确实在看到了这个问题但我正在寻找更通用的解决方案。
1楼
在撰写本文时,PyPDF4 文档的最佳资源是 PyPDF4 的示例代码 -
看看那个可以写一个简单的脚本来做到这一点:
# pdf_rotate_every_page.py
from PyPDF4 import PdfFileReader, PdfFileWriter
from sys import argv, path, stderr
from os.path import abspath, basename, dirname, join
USAGE = "Rotate every page in a PDF. Call script with single pdf file as input argument"
def main():
output_writer = PdfFileWriter()
if len(argv) < 2:
print(USAGE)
exit(1)
else:
inputpath = argv[1].strip()
filename = basename(inputpath)[:-4]
if len(argv) > 2:
output = argv[2].strip()
with open(inputpath, "rb") as inputf:
pdfOne = PdfFileReader(inputf)
numPages = pdfOne.numPages
for i in list(range(0, numPages)):
page = pdfOne.getPage(i).rotateClockwise(180)
output_writer.addPage(page)
with open(filename + "_rotated.pdf", "wb") as outfile:
output_writer.write(outfile)
if __name__ == "__main__":
main()