I have a report generation script (let's call this script A)that generates a PDF report. I want to import this in a new script (script B) and call the functions from within this new script.
The problem:
1. Script A crashes when called from Script B exactly at the creation of a QPixmap object. Python console restarts.
2. Script A runs fine when it is run on its own, but if I try to call it from Command Prompt python.exe crashes.
I will explain and display the code in the next section as the best I have managed to do is identify the problems.
What I have tried:
The following is Script B. All it does is it imports Script A, creates an instance of a class defined in A, and uses that to call a method of that object.
import Script A as et
a = "sample"
mdbPath = "C:\\Python27\\test.mdb"
directory = "C:\\Python27"
ui = et.HawkAutomationScript()
ui.Main(mdbPath,directory,a)
Script A contains the definition of a class called
HawkAutomationScript
. This object contains two functions to note: one Main function and one function to generate the report. These are the imported modules:
from random import*
import sys
import os
import subprocess
import time
from datetime import datetime
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QTextDocument, QPrinter, QApplication, QPainter
from string import Template
import shutil
import time
import pyodbc
This is the definition of the class and the Main function:
doc = QTextDocument()
myCursor = QtGui.QTextCursor(doc)
class HawkAutomationScript(object):
def Main(self,mdb,directory,name):
Ui_FormObject = HawkAutomationScript()
Ui_FormObject.fnGenerateReport(directory,name)
def fnGenerateReport(self,directory,name):
now = datetime.now()
dateString = now.strftime("%d-%b-%Y")
dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\"
ResultName = "SelfTest_Results"
testName = name+".pdf"
subDirPath = dirPath + name
if(os.path.isdir(dirPath)):
if(os.path.isdir(subDirPath)):
subDirPath = subDirPath + testName
else:
os.mkdir(subDirPath)
subDirPath = subDirPath + testName
else:
os.mkdir(dirPath)
os.mkdir(subDirPath)
subDirPath = subDirPath + testName
pixmap = QtGui.QPixmap(doc.size().width(), doc.size().height())
printer = QPrinter()
printer.setOutputFileName(subDirPath)
printer.setOutputFormat(QPrinter.PdfFormat)
doc.print_(printer)
painter = QPainter(pixmap)
painter.begin( printer )
doc.drawContents(painter)
painter.end()
This is all the relevant code.
If I run script B, A crashes at the QPixmap line marked in the code. But if I copy the lines from B and run A on its own, the PDF report is generated. But if I run A on its own from Command Prompt python.exe crashes.
Any help would be really appreciated. Thank you!!