2013-07-31 44 views
2

我有一個使用ReportLab的創建報表的Python腳本,該腳本已經過測試,偶爾我收到以下錯誤消息,但預期工作壞參數內部函數錯誤蟒蛇

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 

線605是createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)

createReport是......創建報告的函數。 我已經通過代碼調試,我沒有得到任何錯誤,什麼可能會使錯誤出現,錯誤只發生在場合,而在其他場合,報告創建罰款。

編輯===== CREATEREPORT代碼

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 2號*

完全回溯這裏

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 

感謝

+0

你能提供完整的堆棧跟蹤嗎?還有一些我們可以看看的代碼? – Paco

+0

@Paco,代碼運行在名爲ArcGIS的外部程序中,它調用腳本,所以我無法獲得完整的堆棧跟蹤。我用創建報告代碼更新了原始問題 - 它有點長... – MapMan

+0

@Paco,完整跟蹤添加到原始問題 – MapMan

回答

0

在我experi因爲ReportLab的間歇性錯誤通常是由於嘗試渲染可能具有「怪異」內容的文本時引起的問題,例如字符集或空值的意外字符。仔細查看你的代碼,我沒有注意到任何明顯會導致問題的東西,但是你可能想要考慮某些事情包含意外值的可能性。

特別是,我會看看處理selContact的代碼,看看它的任何屬性是否意外爲空或者不是可能導致錯誤的字符串。

沒有完整的堆棧跟蹤,很難說更具體的東西。

ETA

基於現在提供堆棧跟蹤,貌似這個問題必須是有東西進入了title變量,你想被用於繪製字符串畫或字體。看着你的代碼,我沒有注意到任何問題,但沒有任何暗示拋出錯誤的部分代碼的間歇性問題。

+0

我在原始問題中添加了完整的跟蹤。 – MapMan

0

這個消息讓我抓狂,在過去幾個星期,我終於想通了兩件事情導致此錯誤,以及如何解決這些問題:

  • 爲G湘沃利III建議,如果你嘗試建立您的報告包含錯誤的元素,可能會顯示。

    修復:找到哪個元素已損壞或在ArcGIS外部調試程序,您將看到更多明確的錯誤消息。

  • 如果你的Python安裝有點搞砸了,它可能會出現。我發生的是,我將ReportLab文件包含在與腳本相同的文件夾中,以便於分發。但我之前也在我的Python安裝文件夾中安裝了ReportLab。不知怎的,那讓ArcGIS困惑。

    修復:刪除Python ?? /(ArcGIS ??/lib/site-packages/reportlab文件夾,以便您的腳本只有一個庫選項。

我希望能幫到別人。