2

I have a python script that uses ReportLab to create a report, the script has been tested and works as expected however on occasions I get the following error message

TraceBack info:

File "C:\tools\LegionellaTool.py", line 605, in <module>

createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)

Error Info:

..\Objects\moduleobject.c:50: bad argument to internal function

Line 605 is createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)

createReport is the function that ...creates the report. I have debugged through the code and I havent got any errors, what might make the error to appear, the error only happens on occasions whereas on other occasions the report is created fine.

EDIT===== createReport code

def createReport(myReportTable, myReportFolder, M3Table, OpenPDF):
    global Author
    global TimeStamp

##    myReportTable = gp.GetParameterAsText(0)
##    myReportFolder = gp.GetParameterAsText(1)
##    M3Table = gp.GetParameterAsText(2)
##    OpenPDF = gp.GetParameterAsText(3)

    Author = getpass.getuser() #gets OS user name
    TimeStamp = str(datetime.datetime.now().hour) + ':' + str(datetime.datetime.now().minute) + ' on the ' + str(datetime.date.today().day) + '/' + str(datetime.date.today().month) + '/' + str(datetime.date.today().year) 

    #add time stamp to file name
    myFile = myReportFolder + os.sep + 'CoolingTowersForInspection_' + str(datetime.datetime.now().hour) + '_' + str(datetime.datetime.now().minute) + '_' + str(datetime.date.today().day) + '_' + str(datetime.date.today().month) + '_' + str(datetime.date.today().year) + '.pdf'
    c = reportlab.pdfgen.canvas.Canvas(myFile)

    #creates sectors array
    sectors = []
    sectors.append('NW')
    sectors.append('NE')
    sectors.append('SW')
    sectors.append('SE')

    for sector in sectors:
        #sector header
        #framePage(c, 'Cooling Towers for Inspection - ' + sector + ' sector')
        title = 'Cooling Towers for Inspection - ' + sector + ' sector'
        c.setFont('Helvetica',20) #title font
        c.drawString(reportlab.lib.units.inch, 10.5 * reportlab.lib.units.inch, title) #creates title

        c.setFont('Helvetica',10) #header and footer font

        #creates header
        c.drawCentredString(4.135 * reportlab.lib.units.inch, 0.75 * reportlab.lib.units.inch,
                                 'Report generated by ' + Author + ' at ' + TimeStamp + ' - Page %d' % c.getPageNumber())

        #creates footer
        c.drawCentredString(4.135 * reportlab.lib.units.inch, 11.00 * reportlab.lib.units.inch,
                                'Environmental Services')

        #draw a border
        c.setStrokeColorRGB(1,0,0)
        c.setLineWidth(5)
        c.line(0.8 * reportlab.lib.units.inch, reportlab.lib.units.inch, 0.8 * reportlab.lib.units.inch, 10.75 * reportlab.lib.units.inch)

        #reset carefully afterwards
        c.setLineWidth(1)
        c.setStrokeColorRGB(0,0,0)

        c.setFont('Helvetica', 10)

        #gets towers in that sector
        myTowers = arcpy.SearchCursor(myReportTable,"\"SECTOR\" = '" + sector + "'","","")
        selTower = myTowers.next()

        y = 730

        if selTower is not None:

            while selTower:
                #insert page break when close to the end of the page
                if y < 110:
                    c.showPage()
                    y = 730
                    framePage(c, 'Cooling Towers for Inspection - ' + sector + ' sector')

                if selTower.TOWER_NAME <> None:
                    c.drawString(100, y, string.strip(selTower.TOWER_NAME))
                else:
                    c.drawString(100, y, "na")
                y = y - 12

                if selTower.TOWER_ADDRESS <> None:
                    c.drawString(100, y, string.strip(selTower.TOWER_ADDRESS))
                else:
                    c.drawString(100, y, "na")
                y = y - 12

                c.drawString(100, y, "Number of towers: " + str(int(selTower.NUMBER_OF_TOWERS)) + " ;   M3 Code: " + selTower.UKEY + " ;   Distance band: " + str(int(selTower.distance)) + " meters")
                y = y - 12

                inspectString = ""
                if selTower.TOWER_RATING <> None:
                    inspectString = "Inspection rating: " + selTower.TOWER_RATING
                else:
                    inspectString = "Inspection rating: na;"

                if selTower.TOWER_LAST_INSPECTION <> None:
                    t = selTower.TOWER_LAST_INSPECTION
                    strLastInspection = t.strftime("%A, %d %b %Y")

                    inspectString = inspectString + "   Last inspection: " + strLastInspection
                    #inspectString = inspectString + "   Last inspection: " + selTower.TOWER_LAST_INSPECTION
                else:
                    inspectString = inspectString + "   Last inspection: na"

                c.drawString(100, y, inspectString)
                y = y - 12

                c.drawString(100, y, "Contacts:")
                y = y - 12

                myTowerUKEY = selTower.UKEY

                #gets contacts for that cooling tower            
                myTowerContacts = arcpy.SearchCursor(M3Table,"\"UKEY\" = '" + myTowerUKEY + "'","","")

                selContact = myTowerContacts.next()

                while selContact:
                    if y < 110:
                        c.showPage()
                        y = 730
                        framePage(c, 'Cooling Towers for Inspection - ' + sector + ' sector')

                    contact = ""
                    if selContact.TITLE <> None:
                        if string.strip(selContact.TITLE) <> "":
                            contact = string.strip(selContact.TITLE) + " "
                    if selContact.FIRSTNAME <> None:
                        contact = contact + selContact.FIRSTNAME + " "
                    if selContact.FAMILYNAME <> None:
                        contact = contact + selContact.FAMILYNAME + " "
                    if selContact.JOBTITLE <> None:
                        contact = contact + "(" + selContact.JOBTITLE + ") "
                    if selContact.TELW <> None:
                        contact = contact + selContact.TELW + "(work) "
                    if selContact.MOBILE <> None:
                        if string.strip(selContact.MOBILE) <> "":
                            contact = contact + string.strip(selContact.MOBILE) + "(mobile) "
                    if selContact.TELH <> None:
                        if string.strip(selContact.TELH) <> "":
                            contact = contact + string.strip(selContact.TELH) + "(home)"

                    contact = string.strip(contact)

                    c.drawString(100, y, contact)
                    y = y - 12
                    selContact = myTowerContacts.next()                

                y = y - 12
                del myTowerContacts
                selTower = myTowers.next()
        else:
            c.drawString(100, y, "no cooling towers for inspection in this sector")

        c.showPage() #insert page break after each sector

    del myTowers

    c.save()

    if OpenPDF == "true":
        os.startfile(myFile)

#function that creates each page
def framePage(canvas, title):
    canvas.setFont('Helvetica',20) #title font
    canvas.drawString(reportlab.lib.units.inch, 10.5 * reportlab.lib.units.inch, title) #creates title

    canvas.setFont('Helvetica',10) #header and footer font

    #creates header
    canvas.drawCentredString(4.135 * reportlab.lib.units.inch, 0.75 * reportlab.lib.units.inch,
                             'Report generated by ' + Author + ' at ' + TimeStamp + ' - Page %d' % canvas.getPageNumber())

    #creates footer
    canvas.drawCentredString(4.135 * reportlab.lib.units.inch, 11.00 * reportlab.lib.units.inch,
                            'Environmental Services')

    #draw a border
    canvas.setStrokeColorRGB(1,0,0)
    canvas.setLineWidth(5)
    canvas.line(0.8 * reportlab.lib.units.inch, reportlab.lib.units.inch, 0.8 * reportlab.lib.units.inch, 10.75 * reportlab.lib.units.inch)

    #reset carefully afterwards
    canvas.setLineWidth(1)
    canvas.setStrokeColorRGB(0,0,0)

EDIT Number 2 *

Full traceback is here

Traceback (most recent call last):
  File "C:\tools\LegionellaTool.py", line 613, in <module>
    createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)

  File "C:\tools\LegionellaTool.py", line 120, in createReport

    c.drawString(reportlab.lib.units.inch, 10.5 * reportlab.lib.units.inch, title) #creates title

  File "c:\python26\arcgis10.0\lib\site-packages\reportlab\pdfgen\canvas.py", line 1481, in drawString

    t.textLine(text)

  File "c:\python26\arcgis10.0\lib\site-packages\reportlab\pdfgen\textobject.py", line 426, in textLine

    self._code.append('%s T*' % self._formatText(text))
  File "c:\python26\arcgis10.0\lib\site-packages\reportlab\pdfgen\textobject.py", line 393, in _formatText

    for f, t in pdfmetrics.unicode2T1(text,[font]+font.substitutionFonts):
SystemError: ..\Objects\moduleobject.c:50: bad argument to internal function

<type 'exceptions.SystemExit'>: 1

Thanks

4

2 に答える 2

0

私の経験では、ReportLab で断続的なエラーが発生する原因のほとんどは、文字セットの予期しない文字や null など、「奇妙な」ものが含まれている可能性のあるテキストをレンダリングしようとするときの問題です。あなたのコードを調べてみると、明らかに問題の原因となるものは何も見つかりませんでしたが、何かに予期しない値が含まれている可能性を調べてみてください。

特に、対処するコードを見てselContact、その属性のいずれかが予期せず null であるか、エラーにつながる可能性のある文字列ではないかどうかを確認します。

完全なスタック トレースがなければ、より具体的なことを言うのは困難です。

ETA

現在提供されているスタック トレースに基づくと、描画しようとしている変数に何かが入っているかtitle、文字列の描画に使用されているフォントに問題があるようです。あなたのコードを見て、私は何の問題にも気付かず、エラーをスローしているコードの部分に断続的な問題があることを示唆するものは何もありません。

于 2013-07-31T13:30:40.713 に答える