9

我正在使用 xlsxwriter 工具将一些数据从用户输入写入 .xlsx。(总的来说,我对编程很陌生,如果我的代码很糟糕,我很抱歉,如果您建议/纠正我犯的任何错误,我们将不胜感激!对不起!)

以下是执行此操作的当前代码:

但是,我在将数据写入新行而不是替换前一行时遇到了一些问题,因此 .xlsx 最终成为用户数据的集合。

import getpass
import os
import xlsxwriter

print('Hello, please answer the follow questions to complete your registration.')

userfirstname = input('Please enter your FIRST name.\n')
print('Thank you, %s' % userfirstname, )

userlastname = input('Please enter your LAST name.\n')
print('Thank you %s' % userfirstname, '%s' % userlastname)

userage = input('Please enter your AGE.\n')
print('Thank you %s' % userfirstname, '%s' % userlastname)

username = input('Please enter your desired USER NAME.\n')
print('Thank you %s' % userfirstname, '%s' % userlastname, 'Your chosen username is %s' %     username)
#Within this section, a database scan must take place to check current used usernames and text  match

userpwd = getpass.getpass('Please enter your desired PASSWORD.\n')
print('Thank you %s' % userfirstname, '%s' % userlastname)
#within this section, a application password authentication must take place to fit password criteria

usermailid = input('Please enter the email address you wish to register with.\n')
print('Your chosen registration email address is: %s' % usermailid)
#Within this section, a database scan must take place to check current registered mail addresses    and text match



#User Database .xlsx
workbook =xlsxwriter.Workbook('UserDatabase.xlsx')
worksheet = workbook.add_worksheet()

worksheet.set_column('A:A',20)
worksheet.set_column('B:B',20)
worksheet.set_column('C:C',20)
worksheet.set_column('D:D',20)
worksheet.set_column('E:E',20)
worksheet.set_column('F:F',20)

worksheet.write('A1', 'First Name')
worksheet.write('B1', 'Last Name')
worksheet.write('C1', 'Age')
worksheet.write('D1', 'User Name')
worksheet.write('E1', 'Password')
worksheet.write('F1', 'Email ID')

worksheet.write_string(  , userfirstname)
worksheet.write_string('', userlastname)
worksheet.write_string('', userage)
worksheet.write_string('', username)
worksheet.write_string('', userpwd)
worksheet.write_string('', usermailid)


workbook.close()

因此,从这里开始,使用列标题创建了一个可爱的 .xlsx,并且用户数据对第二行进行了很好的处理,但是在 worksheet.write_string( 部分我需要知道如何指定如何将字符串写入新行或者可能是一些条件格式,比如“如果该行已经包含文本,则写入下一个空闲行”等。

如果您知道比 xlsxwriter 更好的工具,请随时告诉我!

谢谢 :)

4

2 回答 2

10

更好的方法是使用 write_row。

您的代码将简化为:

row = 0
col = 0
rowHeaders = ['First Name','Last Name','Age','User Name','Password','Email ID']
rowValues = [userfirstname,userlastname,userage,username,userpwd,usermailid ]
worksheet.write_row(row, col,  tuple(rowHeaders))
row += 1
worksheet.write_row(row, col, tuple(rowValues))`

检查链接以获取更多文档: write_row

于 2015-04-13T12:17:03.250 回答
3

在方法中使用 (row, col) 表示法是可能的,而且通常更有用write_

像这样的东西:

row = 0
col = 0

worksheet.write(row, col,     'First Name')
worksheet.write(row, col + 1, 'Last Name')
worksheet.write(row, col + 2, 'Age')
worksheet.write(row, col + 3, 'User Name')
worksheet.write(row, col + 4, 'Password')
worksheet.write(row, col + 5, 'Email ID')

row += 1
worksheet.write_string(row, col,     userfirstname)
worksheet.write_string(row, col + 1, userlastname)
worksheet.write_string(row, col + 2, userage)
worksheet.write_string(row, col + 3, username)
worksheet.write_string(row, col + 4, userpwd)
worksheet.write_string(row, col + 5, usermailid)

请参阅文档的使用单元符号部分。

于 2014-08-10T20:07:12.990 回答