import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyPDF2 import PdfFileReader, PdfFileWriter
from main_form import * class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.actionLoad_File_L.triggered.connect(self.showDialog) def showDialog(self):
infn = QFileDialog.getOpenFileName(self,
'Open file',
'd:') with open(infn, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages() print(info, number_of_pages) if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

原因是getOpenFileName的返回值是文件名文件类型的元组,这里直接把元组赋值给了open函数,但是应该是文件名,自己对于代码基础原理不够理解,实际实例代码都有写

infn = QFileDialog.getOpenFileName(self,
'Open file',
'd:')[0]

我没有仔细观察,花了1个小时解决这个问题

pycharm没有报这个错误,到命令行运行可以很清晰的看到这个错误,pycharm假死,所以pycharm也不可靠啊



所以可以像下面这样写

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyPDF2 import PdfFileReader, PdfFileWriter
from main_form import * class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.actionLoad_File_L.triggered.connect(self.get_info) def get_info(self):
path = 'D:/coding/envs/brisspython/a.pdf'
filename, filetype = QFileDialog.getOpenFileName(self, 'Open file')
with open(filename, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages() print(number_of_pages) if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

或者

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyPDF2 import PdfFileReader, PdfFileWriter
from main_form import * class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.actionLoad_File_L.triggered.connect(self.get_info) def get_info(self):
path = 'D:/coding/envs/brisspython/a.pdf'
filename = QFileDialog.getOpenFileName(self, 'Open file')[0]
with open(filename, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages() print(number_of_pages) if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

pyqt5加载pdf文档失败的更多相关文章

  1. 打开地图文件和shape文件代码加载Mxd文档

    代码加载Mxd文档 用代码添加Mxd文档,用到AxMapControl.LoadMxFile(sFilePath),我们只要将Mxd文档的路径传给这个方法即可 /// <summary>  ...

  2. jQuery使用load方法加载其他文档内容

    A文档载入B文档的内容,并且通过JQ操作被引入到A文档中的元素 A文档 (index.html): <!DOCTYPE html> <html lang="en" ...

  3. C# 加载xml文档文件及加载xml字符串

    //创建XmlDocument对象 XmlDocument xmlDoc = new XmlDocument(); //载入xml文件名 xmlDoc.Load(filename); //如果是xml ...

  4. jQuery的$.getScript方法去加载javaScript文档解析

    1.两个文件的代码如下: <script> function Ajax(){ //将9-4.html中的Ajax()函数进行修改 $.getScript('9-8.js',function ...

  5. iframe加载的文档高度

    var clientHeight = $("#iframe").contents().find("body").height();

  6. 【Win10 开发】读取PDF文档

    关于用来读取PDF文档的内容的API,其实在Win8.1的时候就有,不过没关系,既咱们讨论的是10的UAP,连同8.1的内容也包括进去,所以老周无数次强调:把以前的内容学好了,就可以在不学习任何新知识 ...

  7. C# 打印PDF文档的10种方法

    操作PDF文档时,打印是常见的需求之一.针对不同的打印需求,可分多种情况来进行,如设置静默打印.指定打印页码范围和打印纸张大小.双面打印.黑白打印等等.经过测试,下面将对常见的几种PDF打印需求做一些 ...

  8. Java 设置PDF文档背景色

    一般生成的PDF文档默认的文档底色为白色,我们可以通过一定方法来更改文档的背景色,以达到文档美化以及保护双眼的作用. 以下内容提供了Java编程来设置PDF背景色的方法.包括: 设置纯色背景色 设置图 ...

  9. Apache PDFbox开发指南之PDF文档读取

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51542309 相关文章: <Apache PDFbox开发指南之PDF文本内容 ...

随机推荐

  1. Arduino通信篇系列之print()和write()输出方式的差异

    我们都知道,在HardwareSerial类中有print()和write()两种输出方式, 两个都可以输出数据,但其输出方式并不相同. 例子: float FLOAT=1.23456; int IN ...

  2. 一、Shell概述

    什么是Shell 解释Shell脚本名词之前,我们先来说下什么是Shell? Shell是一个命令解释器,它在操作系统的最底层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种各样的操作系统 ...

  3. RestTemplate远程调用方法

    概述: spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值 ...

  4. qt连接mysql数据库实例

    qt5.2版本已经封装进去了mysql驱动,所以省去了我们现编译的麻烦!!! #include <QCoreApplication> #include <QDebug> #in ...

  5. 文件(file 类)

    题目: 编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件.之后,将这些文件中的某一个文件剪切到另外一个目录中. 代码: File_Demo /** * 包含两个类,一个主 ...

  6. 最小生成树(二)prim

    今天为大家带来最小生成树的第二种实现方式,比起kruskal来说,prim相对要复杂一些,在稠密图的表现中表现较好,最优情况下也是nlogn级别. 描述: 1).输入:一个加权连通图,其中顶点集合为V ...

  7. LeetCode 218. The Skyline Problem 天际线问题(C++/Java)

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

  8. 【译文连载】 理解Istio服务网格(第二章 安装)

    全书目录 第一章 概述 本文目录 1.命令行工具安装 2. Kubernetes/OpenShift安装 3. Istio安装 4.示例Java微服务安装 4.1 源码概览 4.2 编译和部署cust ...

  9. ts的特殊数据类型

    四. Ts数据类型 tuple(元组类型):可以给数组指定位置存指定类型数据 例:let arr:[number, string] = [123, ‘123’]; enum(枚举):将数字转化为标识符 ...

  10. centos7.5下yum安装mysql-5.6.43

    cd ~/ && cat /etc/redhat-release yum list installed |grep mysql #<===查看是否安装mysql,如果已经安装,使 ...