Fill new last row like data base (excel)

1,019 views
Skip to first unread message

Alexandre Goncalves

unread,
May 20, 2017, 7:34:04 PM5/20/17
to python-excel
Hi.

I have code, but this code not save data into next empty last row in file.
I want write new data in last row (empty)
Exemple:
My file has this data
First Name Last Name Age User Name Password Email ID
elaine goncalves 42 nane anne elaine.g...@hotmail.com

If i run this cade, i want insert new empty row.
First Name Last Name Age User Name Password Email ID
elaine goncalves 42 nane anne elaine.g...@hotmail.com
aquino goncalves 34 ale ale aqu...@gmail.com



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)


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

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))
 
workbook.close()

Adrian Klaver

unread,
May 20, 2017, 7:42:07 PM5/20/17
to python...@googlegroups.com, Alexandre Goncalves
On 05/20/2017 07:02 AM, Alexandre Goncalves wrote:
> Hi.
>
> I have code, but this code not save data into next empty last row in file.
> I want write new data in last row (empty)
> Exemple:
> My file has this data
> First Name Last Name Age User Name Password Email ID
> elaine goncalves 42 nane anne elaine.g...@hotmail.com
>
>
> If i run this cade, i want insert new empty row.
> First Name Last Name Age User Name Password Email ID
> elaine goncalves 42 nane anne elaine.g...@hotmail.com
> aquino goncalves 34 ale ale aqu...@gmail.com <mailto:aqu...@gmail.com>
>
>

xlswriter only writes new spreadsheets. It will not open an existing
spreadsheet and add to it, which what you seem to be trying below. As I
see it you have two options:

1) Use openpyxl:
http://www.python-excel.org/

2) Use xlrd/xlutils to read the existing file into a new xlswriter file.
> --

--
Adrian Klaver
adrian...@aklaver.com

Alexandre Goncalves

unread,
May 20, 2017, 9:56:21 PM5/20/17
to python-excel, aquin...@gmail.com
OK, but if use (xlrd/xlutils ), how my code chage to add data after last row?

Adrian Klaver

unread,
May 21, 2017, 9:50:00 AM5/21/17
to python...@googlegroups.com, Alexandre Goncalves
On 05/20/2017 06:56 PM, Alexandre Goncalves wrote:
> OK, but if use (xlrd/xlutils ), how my code chage to add data after last
> row?
>

I would say the easiest thing to do is use openpyxl:

https://pypi.python.org/pypi/openpyxl

https://openpyxl.readthedocs.io/en/default/usage.html


--
Adrian Klaver
adrian...@aklaver.com

Alexandre Goncalves

unread,
May 21, 2017, 12:45:54 PM5/21/17
to python-excel, aquin...@gmail.com
Sorry, but i can't build code to read.
#!/home/elaineluisdesouzagoncalves/anaconda3/bin/python3
# https://media.readthedocs.org/pdf/openpyxl/latest/openpyxl.pdf
from openpyxl import load_workbook

wb
= load_workbook(filename = '/home/elaineluisdesouzagoncalves/Documents/dados2.xlsx')
ws
= wb['tab3']  

tlin
= ws.max_row
tcol
= ws.max_column

dados
= list()
tb
= list()

for linhas in range(tlin):
   
for colunas in range(tcol):
        dados
.append(ws.cell(row = tlin, column = tcol))
    tb
.append(dados)
    dados
= []
    linhas
+= 1
print(tb)
So i can't do code to wirte.

Adrian Klaver

unread,
May 21, 2017, 4:04:49 PM5/21/17
to python...@googlegroups.com, Alexandre Goncalves
On 05/21/2017 09:45 AM, Alexandre Goncalves wrote:
> Sorry, but i can't build code to read.
> |
> #!/home/elaineluisdesouzagoncalves/anaconda3/bin/python3
> # https://media.readthedocs.org/pdf/openpyxl/latest/openpyxl.pdf
> fromopenpyxl importload_workbook
>
> wb =load_workbook(filename
> ='/home/elaineluisdesouzagoncalves/Documents/dados2.xlsx')
> ws =wb['tab3']
>
> tlin =ws.max_row
> tcol =ws.max_column
>
> dados =list()
> tb =list()
>
> forlinhas inrange(tlin):
> forcolunas inrange(tcol):
> dados.append(ws.cell(row =tlin,column =tcol))
> tb.append(dados)
> dados =[]
> linhas +=1
> print(tb)
> |
> So i can't do code to wirte.

I have never used openpyxl before, so take the below as a rough example:

from openpyxl import Workbook

# Create a file and write to it
file_out = 'read_write_test.xlsx'
wb = Workbook()
ws1 = wb.active
ws1.title = 'Test'
for row_idx in range(1, 10):
for col_idx, val in enumerate(['one', 'two', 'three']):
ws1.cell(column=col_idx + 1, row=row_idx, value=val)
wb.save(file_out)

from openpyxl import load_workbook

# Open existing file and modify it.
file_in_out = 'read_write_test.xlsx'
wb = load_workbook(filename = file_in_out)
ws = wb.get_sheet_by_name('Test')
last_row = ws.max_row
for col_idx, val in enumerate(['one', 'two', 'three']):
ws.cell(column=col_idx + 1, row=last_row + 1, value=val)
wb.save(file_in_out)





--
Adrian Klaver
adrian...@aklaver.com

Alexandre Goncalves

unread,
May 21, 2017, 4:31:46 PM5/21/17
to python-excel, aquin...@gmail.com
Hi Adrian Klaver, thank you for feedback.

I give up, becouse i can't adapt code.

Thank you!!

Adrian Klaver

unread,
May 21, 2017, 4:33:33 PM5/21/17
to python...@googlegroups.com, Alexandre Goncalves
On 05/21/2017 01:31 PM, Alexandre Goncalves wrote:
> Hi Adrian Klaver, thank you for feedback.
>
> I give up, becouse i can't adapt code.

What is failing?

Can you show the error messages/tracebacks?

>
> Thank you!!
>


--
Adrian Klaver
adrian...@aklaver.com
Reply all
Reply to author
Forward
0 new messages