I am using python-2.7 and xlsxwriter for writing in excel sheet.
Following is my code...
workbook = Workbook('D:\S_details.xlsx')
sheet = workbook.add_worksheet()
rownum = 2
colnum = 2
for a in student_result:
for r, row in enumerate(student_result):
for c, col in enumerate(row):
bold = workbook.add_format({'bold': 1})
sheet.write('A1','Student_ID',bold)
sheet.write('B1','Student_Link',bold)
sheet.write('C1','Student_Name',bold)
sheet.write('D1','Student_Qualification',bold)
sheet.write('E1','Student_Address',bold)
sheet.write('F1','Student_City',bold)
sheet.write('G1','Student_State',bold)
sheet.write('H1','Student_Country',bold)
sheet.write('I1','Student_Stream',bold)
sheet.write('J1','Student_Gender',bold)
sheet.write(r,c,col)
rownum = rownum + 1
colnum = colnum + 1
the code runs well but the very first entry which is retrieved from database is overwritten by the header of each column.
Hence only first entry is overwritten and rest of the entries are visible perfectly.
I am also printing the data before writing it to excel sheet but it is not showing any error nor the records are duplicated or so.
Can anyone please guide where I am going wrong...
Guidance / Help in any form is welcome.
Thank-you in advance :)
There are a few issues with the code example:
for a in student_result loop is unused.row_num and col_num variables are incremented but not used.enumerate() returns a 0 row value which overwrites or is overwritten by the A1, B1 entries in the headers.Fixing these issues would give something like this:
import xlsxwriter
workbook = xlsxwriter.Workbook('S_details.xlsx')
sheet = workbook.add_worksheet()
# Generate some sample data.
student_result = []
for num in range(1, 11):
student_result.append([num] * 10)
# Make the columns wider so that the text is visible.
sheet.set_column('A:J', 20)
# Add some formatted headers.
bold = workbook.add_format({'bold': 1})
sheet.write('A1','Student_ID',bold)
sheet.write('B1','Student_Link',bold)
sheet.write('C1','Student_Name',bold)
sheet.write('D1','Student_Qualification',bold)
sheet.write('E1','Student_Address',bold)
sheet.write('F1','Student_City',bold)
sheet.write('G1','Student_State',bold)
sheet.write('H1','Student_Country',bold)
sheet.write('I1','Student_Stream',bold)
sheet.write('J1','Student_Gender',bold)
# Write the data.
for row_num, row_data in enumerate(student_result):
for col_num, col_data in enumerate(row_data):
sheet.write(row_num + 1, col_num, col_data)
workbook.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With