我的程序提示用户选择文件(通过PyQT4 MainWindow),然后在ReportLab中生成pdf报告。
我在试图包含的2张图片之一上收到了IOError。两个图像都在我正在使用的每个其他文件所在的目录中。我也尝试将其他图像加载为两个图像,没有错误。我尝试使用目录外的不同图像 - 工作正常。我怀疑文件大小可能太大了问题图像,所以我从450kB图像变为45 kB图像。仍然没有骰子。
问题代码:
#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文件。
答案 0 :(得分:0)
我发现我遇到的问题可以追溯到不使用完整路径名。为了解决这个问题,我保存了一个变量NAMED路径名:
document_title_index = str(first_Fname).rfind('/') + 1 #find the last / and add 1
document_title = first_Fname[document_title_index: len(first_Fname)-4] #uses the index to grab the string that's affiliated with the first data file for naming the report
pathname = first_Fname[0:document_title_index] # grabs the pathname part to place new files correctly
然后,当我保存我的数字时,我使用路径名将它们放在正确的位置 - 使用.py文件本身的报告INSTEAD的源数据,它将在之前结束的位置:
plt.savefig(pathname + figname + '.jpg', dpi = 100) #save plots to the same place as the first data file