为什么我的Python脚本将对象作为字符串返回

时间:2017-08-03 12:21:29

标签: python excel openpyxl

我正在编写一个小的python脚本,将两个excel文件合并为一个。这很简单,但我在updateExcelFile()方法中遇到错误,指出'str'对象没有属性'DACNum'。在excel文件的第71行(第一个空行)上发生此错误。有人能指出这是为什么吗?谢谢!

  

这是完整的追溯:

Traceback (most recent call last):
File "C:\Users\jasonmcclafferty\Desktop\Python\example.py", line 130, in 
<module>
    DACMgr.updateExcelFile(x, row)
  File "C:\Users\jasonmcclafferty\Desktop\Python\example.py", line 109, 
in updateExcelFile
    DACMgr.workbook.active.cell(row=c_row, column=1, 
value=DACReport.DACNum)
AttributeError: 'str' object has no attribute 'DACNum'
[Finished in 3.3s with exit code 1]
import openpyxl
import os


print("Current directory: " + os.getcwd())
print()

class DACReport:
    DACNum = ''
    PlanNum = ''
    CNNum = ''
    FSCNum = ''
    Name = ''
    AppDetails = ''
    Agent = ''
    DevAddress = ''
    DateRec = ''
    Deadline = ''
    Decision = '' 
    FIReq = ''
    Fee = ''

    def __init__(self, DACNum, Name):
        self.DACNum = DACNum
        self.Name = Name

    def __str__(self):
        return 'Application #: ' + self.DACNum + ' Applicant Name: ' + self.Name


class DACReader:

    workbook = ''

    def __init__(self, workbook):
        self.workbook = workbook

    ## Reads the record at the given row, creates a DACEntry object from it and prints the string representation.
    def get_record_ar(self, c_row):
        w_bk = self.workbook
        w_sht = w_bk.active


        c_DAC = w_sht.cell(row=c_row, column=1).value
        c_Name = w_sht.cell(row=c_row, column=2).value

        issues = []

        if (c_DAC != None):
            if (c_Name == None):
                issues.append(DACReport(c_DAC, c_Name))

            else:
                tmp = DACReport(c_DAC, c_Name)
        else:
            return 0

        ## Object is printed and returned here, should also be written to excel

        if tmp != None:
            DACMgr.updateDACList(tmp)

            return tmp

    ## Reads the record at the given row, creates a DACRecord object from it and prints the string representation.
    def get_record_old(self, c_row):
        w_bk = self.workbook
        w_sht = w_bk.active
        tmp = ''

        c_DAC = w_sht.cell(row=c_row, column=5).value
        c_Name = w_sht.cell(row=c_row, column=6).value

        issues = []

        if (c_DAC != None):
            if (c_Name == None):
                issues.append(DACReport(c_DAC, c_Name))

            else:
                tmp = DACReport(c_DAC, c_Name)
        else:
            return 0

        ## Object is printed and returned here, should also be written to excel
        if tmp != None:
            DACMgr.updateDACList(tmp)

            return tmp


class DACMgr:

    DAClist = []

    workbook = openpyxl.load_workbook("daccomplete.xlsx")

    def __init__(self, DAClist):
        self.DAClist = DAClist

    def updateDACList(DACReport):
        #print(DACReport)
        DACMgr.DAClist.append(DACReport)
        return 0

    # Updates the excel file with a DACReport at row = c_row
    def updateExcelFile(DACReport: DACReport, c_row: int):
            print(DACReport)
            DACMgr.workbook.active.cell(row=c_row, column=1, value=DACReport.DACNum)
            DACMgr.workbook.active.cell(row=c_row, column=2, value=DACReport.Name)

            openpyxl.writer.excel.save_workbook(DACMgr.workbook, "daccomplete.xlsx")    




aReader = DACReader(openpyxl.load_workbook('ardalsdacregister.xlsx'))
oReader = DACReader(openpyxl.load_workbook('olddacregister.xlsx'))


for x in range(3, 360,1):
    oReader.get_record_old(x)

for x in range(8,150,1):
    aReader.get_record_ar(x)

row = 1
for x in DACMgr.DAClist:
    print(x)
    DACMgr.updateExcelFile(x, row)
    row+=1

`

2 个答案:

答案 0 :(得分:0)

从函数中删除self并尝试...

def updateExcelFile(DACReport: DACReport, c_row: int):
        print(DACReport)
        DACMgr.workbook.active.cell(row=c_row, column=1, value=DACReport.DACNum)
        DACMgr.workbook.active.cell(row=c_row, column=2, value=DACReport.Name)

        openpyxl.writer.excel.save_workbook(DACMgr.workbook, "daccomplete.xlsx")    

答案 1 :(得分:0)

您最后的代码会将DAClist的元素发送给updateExcelFile

for x in DACMgr.DAClist:
    print(x)
    DACMgr.updateExcelFile(x, row)
    row+=1

同时,updateExcelFile期望输入为具有特定属性的对象... value=DACReport.DACNum

看起来在填充DAClist时,某些情况下会包含字符串,而不是具有正确属性的对象。

您的get*方法包含此行......

DACMgr.updateDACList(tmp)

更高,您设置了默认值... tmp = ''

您应该将默认值tmp设为DACReport对象,或者在updateExcelFile中,您应该在尝试访问其中一个之前检查是否正在处理DACReport属性。