Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 830
Ashley Redman BSc Avatar asked Aug 08 '14 15:08

Ashley Redman BSc


1 Answers

A better way to do this would be using write_row.

Your code would be reduced to:

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

Check link for more documentation: write_row

like image 97
Vaulstein Avatar answered Sep 24 '22 02:09

Vaulstein