如何从网址而不是从磁盘
打开pdf像
这样的东西input1 = PdfFileReader(file("http://example.com/a.pdf", "rb"))
我想从网上打开几个文件并下载所有文件的合并。
答案 0 :(得分:13)
我认为urllib2会让你得到你想要的东西。
from urllib2 import Request, urlopen
from pyPdf import PdfFileWriter, PdfFileReader
from StringIO import StringIO
url = "http://www.silicontao.com/ProgrammingGuide/other/beejnet.pdf"
writer = PdfFileWriter()
remoteFile = urlopen(Request(url)).read()
memoryFile = StringIO(remoteFile)
pdfFile = PdfFileReader(memoryFile)
for pageNum in xrange(pdfFile.getNumPages()):
currentPage = pdfFile.getPage(pageNum)
#currentPage.mergePage(watermark.getPage(0))
writer.addPage(currentPage)
outputStream = open("output.pdf","wb")
writer.write(outputStream)
outputStream.close()
答案 1 :(得分:4)
好吧,您可以先单独下载pdf,然后使用pypdf进行阅读
import urllib
url = 'http://example.com/a.pdf'
webFile = urllib.urlopen(url)
pdfFile = open(url.split('/')[-1], 'w')
pdfFile.write(webFile.read())
webFile.close()
pdfFile.close()
base = os.path.splitext(pdfFile)[0]
os.rename(pdfFile, base + ".pdf")
input1 = PdfFileReader(file(pdfFile, "rb"))
答案 2 :(得分:0)
对于 python 3.8
import example1
class Logic:
def mymethod(self):
if self.a > self.b:
example1.message1()
else:
example1.message2()