简体   繁体   English

如何避免使用python覆盖excel中的单元格?

[英]How to avoid overwriting of cell in excel using python?

I am using python-2.7 and xlsxwriter for writing in excel sheet. 我使用python-2.7和xlsxwriter在excel表中编写。

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. 我也在将数据写入excel表之前打印数据,但它没有显示任何错误,也没有重复记录。

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: 代码示例存在一些问题:

  • The headers are re-written for every iteration of the inner loop. 为内循环的每次迭代重写头。 This part of the code should be outside the loop. 这部分代码应该在循环之外。
  • The for a in student_result loop is unused. for a in student_result循环中未使用。
  • The row_num and col_num variables are incremented but not used. row_numcol_num变量递增但未使用。
  • The enumerate() returns a 0 row value which overwrites or is overwritten by the A1 , B1 entries in the headers. enumerate()返回一个0行值,该值将被标题中的A1B1条目覆盖或覆盖。

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

You preset rownum and colnum, but you're not using them in the write statement. 您预设了rownum和colnum,但是您没有在write语句中使用它们。 How about: 怎么样:

   sheet.write(rownum,colnum,col)

Also you probably don't want to advance rownum in the col for loop, so: 你也许不想在col for循环中推进rownum,所以:

 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(rownum,colnum,col)
                    colnum = colnum + 1
              rownum += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM