使用python检测空白的Excel单元。

时间:2022-01-06 22:21:21

I have written a python code for converting data from multiple excel files to single csv file however it is not working. I tried XL_CELL_EMPTY or XL_CELL_BLANK in place of None in following program. But nothing is working. It is giving following error

我编写了一个python代码,用于将数据从多个excel文件转换为单个csv文件,但它不起作用。我尝试了XL_CELL_EMPTY或XL_CELL_BLANK来代替后面的程序中的None。但没有什么是工作。它会产生以下错误

Traceback (most recent call last):
  File "Excel_to_CSV_1.py", line 35, in <module>
    while Sh_B1.cell(i,j).value  != None : 
  File "/usr/lib/pymodules/python2.7/xlrd/sheet.py", line 245, in cell
    self._cell_types[rowx][colx],
IndexError: array index out of range

This is the Program:

这是这个项目:

import xlrd
import os.path
from xlrd import open_workbook,empty_cell
book1 = xlrd.open_workbook("Excel_1.xls")
Sh_B1 = book1.sheet_by_index(0)
book2 = xlrd.open_workbook("Excel_2.xls")
Sh_B2 = book2.sheet_by_index(0)
i = 1
j = 1 
file = open("Output.txt", "w")
while Sh_B1.cell(i,j).value != None :
    while Sh_B1.cell(i,j).value  != None : 
        D1 = Sh_B1.cell(i,j).value
        D2 = Sh_B2.cell(i,j).value
        DB1 = str(D1)+ "," + str(D2)
        file.write("".join(DB1+ "\n"))
        j = j + 1
    j = 1
    print "j=1" 
    i = i + 1
file.close

Where am I going wrong?

我哪里做错了?

1 个解决方案

#1


5  

the Sheetobject of xlrd has a nrowsand a ncols attributes that makes possible to iterate on cells.

xlrd的Sheetobject有一个nrowand一个ncols属性,可以对单元格进行迭代。

import xlrd
book = xlrd.open_workbook("my_file.xls")
sheet = book.sheet_by_index(0) # or: sheet_by_name(Sheet 1")
for row_index in xrange(sheet.nrows):
    for col_index in xrange(sheet.ncols):
        value = sheet.cell(rowx=row_index,colx=col_index).value

#1


5  

the Sheetobject of xlrd has a nrowsand a ncols attributes that makes possible to iterate on cells.

xlrd的Sheetobject有一个nrowand一个ncols属性,可以对单元格进行迭代。

import xlrd
book = xlrd.open_workbook("my_file.xls")
sheet = book.sheet_by_index(0) # or: sheet_by_name(Sheet 1")
for row_index in xrange(sheet.nrows):
    for col_index in xrange(sheet.ncols):
        value = sheet.cell(rowx=row_index,colx=col_index).value