【发布时间】:2014-08-15 20:35:32
【问题描述】:
我的程序提示用户选择文件(通过 PyQT4 主窗口),然后在 ReportLab 中生成 pdf 报告。
我在尝试包含的 2 张图片中的一张出现 IOError。这两个图像都在我正在使用的所有其他文件所在的目录中。我还尝试将另一张图像作为两个图像加载,没有错误。我使用目录外的不同图像进行了尝试 - 工作正常。我怀疑问题图像的文件大小可能太大,所以我从 450kB 图像转到 45kB 图像。还是没有骰子。
问题代码:
#from the code for the Compiled Window - this is how I acquire the images
...
def logo_pic(self):
self.Logo_picture = unicode(QtGui.QFileDialog.getOpenFileName())
self.LogoLabel.setText(self.Logo_picture) #this shows the user the path of the file they selected
def setup_pic(self):
self.setup_picture = unicode(QtGui.QFileDialog.getOpenFileName())
self.SetupLabel.setText(self.setup_picture)
...
#take the file path and turn it into a workable file name
setup_pic_title_index = str(cw.setup_picture).rfind('/') + 1
setup_pic_title = cw.setup_picture[setup_pic_title_index:]
Logo_picture_title_index = str(cw.Logo_picture).rfind('/') + 1
Logo_picture_title = cw.Logo_picture[Logo_picture_title_index:]
#this is where the image itself is pulled and scaled appropriately for the ReportLab document, and added to the list of items to include
elements.append(get_image('./' + Logo_picture_title, width = 9*inch))
elements.append(get_image('./' + setup_pic_title, width = 8*inch))
这是我得到的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/moi/etc", line 367, in <module>
elements.append(get_image('./' + setup_pic_title, width = 8*inch))
File "C:/Users/moi/etc", line 233, in get_image
img = utils.ImageReader(path)
File "C:\Python27\lib\site-packages\reportlab\lib\utils.py", line 585, in __init__
annotateException('\nfileName=%r identity=%s'%(fileName,self.identity()))
File "C:\Python27\lib\site-packages\reportlab\lib\utils.py", line 549, in __init__
self.fp = open_for_read(fileName,'b')
File "C:\Python27\lib\site-packages\reportlab\lib\utils.py", line 452, in open_for_read
raise IOError('Cannot open resource "%s"' % name)
IOError: Cannot open resource "./DSCN2223.JPG"
fileName=u'./DSCN2223.JPG' identity=[ImageReader@0x70b1870 filename=u'./DSCN2223.JPG']
我包含了徽标和设置图片的代码 - 信息基本相同。此外,它适用于目录之外的不同 .jpg 文件。
【问题讨论】:
-
./DSCN2223.JPG在哪里? -
在与我让用户选择的其他 8 个文件相同的目录中。并且和logo jpg在同一目录下。
-
您打开其他任何一个都没有问题?
-
没有。我也可以为这两个文件使用徽标,并且可以正常工作。
-
可以单独打开吗?也就是说,启动 python 解释器并运行非常简化的代码?
标签: python python-2.7 pyqt4 reportlab