AttributeError: 'str' object has no attribute 'write' - Print application answers into Excel
AttributeError: 'str' object has no attribute 'write' - Print application answers into Excel
我正在尝试将 python 应用程序的输出数据打印到 excel 文件中。通过将相关名称写入 A1、B2 等,我能够使用以下代码创建关联列。如何让这些输出输入下面的行,但前提是那里没有其他信息?
workbook = xlsxwriter.Workbook(
'C:\Users\W\Project\Interface\Portal Information Store.xlsx')
worksheet = ("Portal Data")
worksheet.write('A2', self.txt_fname.get(), self.txt_lname.get())
worksheet.write('B2', self.txt_contact.get())
worksheet.write('C2', self.txt_email.get())
worksheet.write('D2', self.txt_pass.get())
worksheet.write('E2', self.txt_confpass.get())
worksheet.write('F2', self.txt_dob.get())
worksheet.write('G2', self.txt_newloc.get())
workbook.close()
我不断收到错误代码:
File "C:\Users\W\Project\Interface\Portal.py", line 109, in register_data
worksheet.write('A2', self.txt_fname.get(), self.txt_lname.get())
AttributeError: 'str' object has no attribute 'write'
谢谢!
我以前没有用过这个库,但是,我知道一个类似的库 - openpyxl
from openpyxl import load_workbook
wb = load_workbook(filename='C:\Users\W\Project\Interface\Portal Information Store.xlsx')
您可以继续赋值如下:
wb["A2"]=self.txt_fname.get() + self.txt_lname.get()
wb["B2"]=self.txt_contact.get()
要检查单元格是否为空,请执行以下操作:
if wb["A2"].value=="":
wb["A2"]=self.txt_fname.get() + self.txt_lname.get() // or whatever action needed
我正在尝试将 python 应用程序的输出数据打印到 excel 文件中。通过将相关名称写入 A1、B2 等,我能够使用以下代码创建关联列。如何让这些输出输入下面的行,但前提是那里没有其他信息?
workbook = xlsxwriter.Workbook(
'C:\Users\W\Project\Interface\Portal Information Store.xlsx')
worksheet = ("Portal Data")
worksheet.write('A2', self.txt_fname.get(), self.txt_lname.get())
worksheet.write('B2', self.txt_contact.get())
worksheet.write('C2', self.txt_email.get())
worksheet.write('D2', self.txt_pass.get())
worksheet.write('E2', self.txt_confpass.get())
worksheet.write('F2', self.txt_dob.get())
worksheet.write('G2', self.txt_newloc.get())
workbook.close()
我不断收到错误代码:
File "C:\Users\W\Project\Interface\Portal.py", line 109, in register_data
worksheet.write('A2', self.txt_fname.get(), self.txt_lname.get())
AttributeError: 'str' object has no attribute 'write'
谢谢!
我以前没有用过这个库,但是,我知道一个类似的库 - openpyxl
from openpyxl import load_workbook
wb = load_workbook(filename='C:\Users\W\Project\Interface\Portal Information Store.xlsx')
您可以继续赋值如下:
wb["A2"]=self.txt_fname.get() + self.txt_lname.get()
wb["B2"]=self.txt_contact.get()
要检查单元格是否为空,请执行以下操作:
if wb["A2"].value=="":
wb["A2"]=self.txt_fname.get() + self.txt_lname.get() // or whatever action needed