Search code examples
excelpython-3.xxlsxwriter

Python Xlsx Writer - Write String to new row


I am using the xlsxwriter tool to write some data to a .xlsx from the user input. (I am very new to programming in general so sorry if my code is awful, it would be appreciated if you suggested / corrected any mistakes I have made! sorry!)

Below is current code to do so:

However I am having a few problems writing the data to a new row rather than replace the previous, so the .xlsx ends up being a collection of user data.

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()

So from here, a lovely .xlsx is created with the column headers and the user data does to the second row which is great but in the worksheet.write_string( section I need to know how to specify how to write the string to a new row or maybe some conditional formatting to say 'if the row already contains text then write to the next free row' etc.

If you know a better tool for this rather than xlsxwriter, feel free to let me know!

Thank you :)


Solution

  • It is possible, and generally more useful to use (row, col) notation in the write_ methods.

    Something like this:

    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)
    

    See the Working with Cell Notation section of the documentation.