I am trying to find a string and copy the lines that have that string
in it to another excel file.
My issue now I can only copy the cells that come after the cell with
the string I flag. Ive tried to store out each line into a temporary
list (not good for speed or memory) but I cant, I think because each
time the "def cell" is called everything has to be defined anew. So
if I try writing the new cell to an array I get an error that I am
trying to access before assigning. I am the original poster and got a
closer to working script which is pasted below. (after script I
pasted a test.xls small grid I am rtesting script on).
import os
from xlutils.filter import \
BaseReader,BaseFilter,BaseWriter,process
class Reader(BaseReader):
def get_filepaths(self):
return [os.path.abspath('test.xls')]
class Writer(BaseWriter):
def get_stream(self,filename):
return file(filename,'wb')
class Filter(BaseFilter):
pending_row = None
wtrowxi = 0
def workbook(self,rdbook,wtbook_name):
self.next.workbook(rdbook,'filtered-'+wtbook_name)
def row(self,rdrowx,wtrowx):
self.pending_row = (rdrowx,wtrowx)
def cell(self,rdrowx,rdcolx,wtrowx,wtcolx):
if rdrowx==0 and rdcolx==0:
self.print_row_num=-1
value = self.rdsheet.cell(rdrowx,rdcolx).value
if value == 'x':
self.print_row_num = rdrowx
self.print_row = True
rdrowx, wtrowx = self.pending_row
self.next.row(rdrowx,wtrowx+self.wtrowxi)
self.wtrowxi -= 1
else:
self.print_row = False
if (rdrowx == self.print_row_num):
self.next.cell(rdrowx,rdcolx,wtrowx+self.wtrowxi,wtcolx)
<sagear...@gmail.com> wrote:
> I dont know if the board group leaders and get replys to this email but I
> just posted a question and thought of a better way of distilling it but
> didnt want to waste board space posting again. My question comes down to
> what the line
> self.wtrowxi -= 1
> does. It somehow flags the whole row not just the cell which is what I
> want. But I get error messages because it sets a row value negative bcause
> of the way my code is. Im betting this email will bounce back so will stop
> typing to myself now:)
> -Sage
> On Thu, Oct 22, 2009 at 8:33 AM, Sage <sagear...@gmail.com> wrote:
> > Sorry for the dumb questions I am new at this and struggling. I
> > want to have a program that will strip all lines from a excel file if
> > the line has a word in it (eventually Id like to be able to run this
> > and have it work on all files in a directory and append to same excel
> > but those are problems for later). Now I am only finding and copying
> > over the cells that I search for, below is my example code where I
> > search for an "x"
> > from xlutils.filter import \
> > BaseReader,BaseFilter,BaseWriter,process
> --
> "Every man is guilty of all the good he did not do."
> "If we believe absurdities, we shall commit atrocities"
> "Common sense is not so common"
> -Voltaire
You don't appear to actually use "print_row" at all...
How about the following instead:
class Filter(BaseFilter):
pending_row = None wtrowxi = 0 filtered = False
def flush(self): if self.pending_row and not self.filtered: rdrowx,wtrowx = self.pending_row wtrowx += self.wtrowxi self.next.row(rdrowx,wtrowx) for cell in self.cells: rdcolx,wtcolx = cell self.next.cell(rdrowx,rdcolx,wtrowx,wtcolx)