pyExcelerator - Protecting Cells

Chris cwitts at gmail.com
Fri Feb 9 02:36:19 EST 2007


I'm sitting with a bit of an issue with pyExcelerator and creating an
Excel file with certain cells protected while the rest of the
spreadsheet is password protected.

The Protection class under Formatting has 2 variables for cell_locked
and formula_hidden, tbh I only need to alter cell_locked to 0 to make
those cells writable but changing that on a global scale ends up with
everything I write being "writeable" if you re-open the file after it
has been produced.

I decided to import Formatting into the Worksheet module like this:
        import Formatting
        self.Formatting = Formatting.Protection

        self.__cell_protect = 1
        self.__formula_hidden = 0

which is the defaults in the Protection class anyway but now when I do
my create file routine when I try to change the cell_protect variable
to 0 it makes absolutely no effect.  The code has been written as
such:

            if protection:
                work_sheet.set_protect(protection)
                work_sheet.set_password(password)
            else:
                pass

            for each_heading in each_work_sheet[1]:
                work_sheet.write(7, heading_cnt, str(each_heading),
header_style)
                heading_cnt += 1

            vert_cnt = 8

            for each_set in each_work_sheet[2]:
                horiz_cnt = 0

                for data_set in each_set:
                    work_sheet.cell_protect = 1
                    work_sheet.formula_hidden = 1

                    if len(str(data_set)) < 1:
                        work_sheet.cell_protect = 0
                        work_sheet.formula_hidden = 0
                        work_sheet.write(vert_cnt, horiz_cnt, ' ')
                        horiz_cnt += 1
                    else:
                        work_sheet.write(vert_cnt, horiz_cnt,
str(data_set), data_style)
                        horiz_cnt += 1

                vert_cnt += 1

As you can see I only want to be able to write to cells that have a
string '' which is parsed correctly through to data which was
originally extracted from a database.  The problem is that I can only
seem to set it to protect all written cells or not.

Any advice or help would be most appreciated. :)
Chris




More information about the Python-list mailing list