BIFFWriter.py :  » XML » pyXLWriter » pyXLWriter-0.4a3 » pyXLWriter » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » XML » pyXLWriter 
pyXLWriter » pyXLWriter 0.4a3 » pyXLWriter » BIFFWriter.py
# pyXLWriter: A library for generating Excel Spreadsheets
# Copyright (c) 2004 Evgeny Filatov <fufff@users.sourceforge.net>
# Copyright (c) 2002-2004 John McNamara (Perl Spreadsheet::WriteExcel, ver .42)
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#----------------------------------------------------------------------------
# This module was ported from PERL Spreadsheet::WriteExcel module.
# The author of the Spreadsheet::WriteExcel module is John McNamara
# <jmcnamara@cpan.org>
#----------------------------------------------------------------------------
# See the README.txt distributed with pyXLWriter for more details.

"""pyXLWriter.BIFFWriter

This module is used in conjunction with Spreadsheet::WriteExcel.

"""
__revision__ = """$Id: BIFFWriter.py,v 1.18 2004/08/20 05:16:16 fufff Exp $"""


from struct import pack


class BIFFWriter:
    """BIFFwriter - An abstract base class for Excel workbooks and
    worksheets.

    """

    BIFF_version = 0x0500

    def __init__(self):
        """Constructor."""
        self._data = ""
        self._datasize = 0
        self._limit = 2080

    def _prepend(self, *args):
        """General storage function."""
        joined = "".join(args)  
        self._data = joined + self._data
        self._datasize += len(joined)

    def _append(self, *args):
        """General storage function."""
        joined = "".join(args) 
        self._data += joined
        self._datasize += len(joined)

    def _store_eof(self):
        """Writes Excel EOF record to indicate the end of a BIFF stream."""
        record = 0x000A
        length = 0x0000
        header = pack("<HH", record, length)
        self._append(header)

    def _store_bof(self, type=0x0005):
        """Writes Excel BOF record to indicate the beginning of a stream or
        sub-stream in the BIFF file.

        type = 0x0005, Workbook
        type = 0x0010, Worksheet

        """
        name = 0x0809
        length = 0x0008
        version = self.BIFF_version
        # According to the SDK build and year should be set to zero.
        # However, this throws a warning in Excel 5. So, use these
        # magic numbers.
        build = 0x096C
        year = 0x07C9
        header = pack("<HH", name, length)
        data = pack("<HHHH", version, type, build, year)
        self._prepend(header, data)

    def _add_continue(self, data):
        """Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes.
        In Excel 97 the limit is 8228 bytes. Records that are longer than these limits
        must be split up into CONTINUE blocks.

        This function take a long BIFF record and inserts CONTINUE records as
        necessary.

        """
        data = data
        limit = self._limit
        record = 0x003C     # Record identifier
        # The first 2080/8224 bytes remain intact. However, we have to change
        # the length field of the record.
        tmp = data[0:limit]
        data = data[limit:]
        tmp = tmp[:2] + pack("<H", limit-4) + tmp[4:]
        # Strip out chunks of 2080/8224 bytes +4 for the header.
        while (len(data) > limit):
            header = pack("<HH", record, limit)
            tmp += header
            tmp += data[0:limit] 
            data = data[limit:]
        # Mop up the last of the data
        header = pack("<HH", record, len(data))
        tmp += header
        tmp += data
        return tmp
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.