我正在编写一个小python脚本,将两个excel文件合并为一个。这相当简单,但我在
updateExcelFile()
以下是完整的回溯:
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
`