我有一个pdf文件“myFile.pdf”。我想使用pyPdf来颠倒其网页的顺序。怎么样?
答案 0 :(得分:4)
from pyPdf import PdfFileWriter, PdfFileReader
output_pdf = PdfFileWriter()
with open(r'input.pdf', 'rb') as readfile:
input_pdf = PdfFileReader(readfile)
total_pages = input_pdf.getNumPages()
for page in xrange(total_pages - 1, -1, -1):
output_pdf.addPage(input_pdf.getPage(page))
with open(r'output.pdf', "wb") as writefile:
output_pdf.write(writefile)
答案 1 :(得分:2)
感谢您分享建议。在选择和保存文件时,我使用了它们并进行了一些编辑,以使界面更加图形化。所有这一切的新内容,以及我添加的内容可能不是高效或干净的,但它对我有用,并认为可以分享。
from PyPDF2 import PdfFileWriter, PdfFileReader
import tkinter as tk
from tkinter import filedialog
import ntpath
import os
output_pdf = PdfFileWriter()
# grab the location of the file path sent
def path_leaf(path):
head, tail = ntpath.split(path)
return head
# graphical file selection
def grab_file_path():
# use dialog to select file
file_dialog_window = tk.Tk()
file_dialog_window.withdraw() # hides the tk.TK() window
# use dialog to select file
grabbed_file_path = filedialog.askopenfilename()
return grabbed_file_path
# file to be reversed
filePath = grab_file_path()
# open file and read
with open(filePath, 'rb') as readfile:
input_pdf = PdfFileReader(readfile)
# reverse order one page at time
for page in reversed(input_pdf.pages):
output_pdf.addPage(page)
# graphical way to get where to select file starting at input file location
dirOfFileToBeSaved = path_leaf(filePath)
locationOfFileToBeSaved=filedialog.asksaveasfilename(initialdir=dirOfFileToBeSaved, initialfile='name of reversed file.pdf',title="Select or type file name and location", filetypes=[("pdf files", "*.pdf")])
# write the file created
with open(locationOfFileToBeSaved, "wb") as writefile:
output_pdf.write(writefile)
# open the file when done
os.startfile(locationOfFileToBeSaved)
答案 2 :(得分:0)
从2019年1月开始(很早),pyPdf不再更新,并且经过测试与(至少)Python 3.6不兼容,并且可能与Python 3完全不兼容。
In [1]: import pyPdf
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-bba5a42e9137> in <module>
----> 1 import pyPdf
c:\temp\envminecart\lib\site-packages\pyPdf\__init__.py in <module>
----> 1 from pdf import PdfFileReader, PdfFileWriter
2 __all__ = ["pdf"]
ModuleNotFoundError: No module named 'pdf'
(将__all__
分配移到import
上方可解决此特定问题,但由于Python 2语法,其他SyntaxError
会弹出。)
幸运的是,它的后续项目PyPDF2在Python 3.6(至少)上可以正常工作。看来核心的面向用户的API是有意维护的,以便与pyPdf兼容,因此pip install PyPDF2
之后nosklo's answer可以在{{1 }}语句,然后将PyPDF2
切换为import
:
xrange
我还建议使用更加Pythonic的方法,即直接使用reversed
遍历页面:
range
我不知道这个from PyPDF2 import PdfFileWriter, PdfFileReader
output_pdf = PdfFileWriter()
with open(r'input.pdf', 'rb') as readfile:
input_pdf = PdfFileReader(readfile)
total_pages = input_pdf.getNumPages()
for page in range(total_pages - 1, -1, -1):
output_pdf.addPage(input_pdf.getPage(page))
with open(r'output.pdf', "wb") as writefile:
output_pdf.write(writefile)
集合是否在原始from PyPDF2 import PdfFileWriter, PdfFileReader
output_pdf = PdfFileWriter()
with open('input.pdf', 'rb') as readfile:
input_pdf = PdfFileReader(readfile)
for page in reversed(input_pdf.pages):
output_pdf.addPage(page)
with open('output.pdf', "wb") as writefile:
output_pdf.write(writefile)
中可用,但是可以说,这实际上并不重要。