我在编程方面经验不足。我想要做的是随机地随机播放pdf的页面并将其输出到另一个pdf。
在线搜索我发现了以下两种解决方案(source 1,source 2):
#!/usr/bin/env python2
import random, sys
from PyPDF2 import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
pages = range(input.getNumPages())
random.shuffle(pages)
for i in pages:
output.addPage(input.getPage(i))
output.write(sys.stdout)
这一个:
#!/usr/bin/python
import sys
import random
from pyPdf import PdfFileWriter, PdfFileReader
# read input pdf and instantiate output pdf
output = PdfFileWriter()
input1 = PdfFileReader(file(sys.argv[1],"rb"))
# construct and shuffle page number list
pages = list(range(input1.getNumPages()))
random.shuffle(pages)
# display new sequence
print 'Reordering pages according to sequence:'
print pages
# add the new sequence of pages to output pdf
for page in pages:
output.addPage(input1.getPage(page))
# write the output pdf to file
outputStream = file(sys.argv[1]+'-mixed.pdf','wb')
output.write(outputStream)
outputStream.close()
我尝试了两者(以及PyPDF2和pyPdf)并且两者都确实创建了一个新的pdf文件,但是这个文件只是空的(并且有0KB)(当我输入时,让我们说" shuffle .py new.pdf")。
我使用PyCharm并且我遇到的一个问题(并不是真正理解)是它说:"找不到参考' PdfFileWriter'"。
PyCharm tells me that it cannot find the reference
我很感激任何帮助,了解我做错了什么:)
修改 正如Tom Dalton所建议的那样,我发布了当我运行第一个时发生的事情:
C:\Users\Anwender\AppData\Local\Temp\shuffle.py\venv\Scripts\python.exe "E:/Shuffle PDF/shuffle.py"
PdfReadWarning: PdfFileReader stream/file object is not in binary mode. It may not be read correctly. [pdf.py:1079]
Traceback (most recent call last):
File "E:/Shuffle PDF/shuffle.py", line 5, in <module>
input = PdfFileReader(sys.stdin)
File "C:\Python27\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
self.read(stream)
File "C:\Python27\lib\site-packages\PyPDF2\pdf.py", line 1689, in read
stream.seek(-1, 2)
IOError: [Errno 22] Invalid argument
Process finished with exit code 1
从评论中我推断,创建新PDF的事实只是因为我打字&#34; shuffle.py newfile.pdf&#34;进入终端:D
编辑2:我现在想出来了;这现在有效:
from PyPDF2 import PdfFileReader, PdfFileWriter
import random, sys
output = PdfFileWriter()
input = PdfFileReader(file("test.pdf", "rb"))
pages = range(input.getNumPages())
random.shuffle(pages)
for i in pages:
output.addPage(input.getPage(i))
outputStream = file(r"output2.pdf", "wb")
output.write(outputStream)
outputStream.close()