datecalc2.py :  » XML » pyXLWriter » pyXLWriter-0.4a3 » examples » 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 » examples » datecalc2.py
#!/usr/bin/env python
# This example script was ported from Perl Spreadsheet::WriteExcel module.
# The author of the Spreadsheet::WriteExcel module is John McNamara
# <jmcnamara@cpan.org>

__revision__ = """$Id: datecalc2.py,v 1.3 2004/07/01 17:14:18 fufff Exp $"""

###############################################################################
#
# Example of how to using the Date::Calc module to calculate Excel dates.
#
# See also the excel_date1.pl example.
#
# These functions have been superceded by Spreadsheet::WriteExcel::Utility.
#
# reverse('(c)'), June 2001, John McNamara, jmcnamara@cpan.org
#

import pyXLWriter as xl
from datetime import datetime# Needs python 2.3


def excel_date(date):
    """Create an Excel date in the 1900 format. All of the arguments are optional
    but you should at least add years.

    Corrects for Excel's missing leap day in 1900. See excel_time1.pl for an
    explanation.
    
    """
    epoch = datetime(1899, 12, 31, 0, 0, 0)
    delta = date - epoch
    xldate = delta.days + float(delta.seconds) / (24*60*60)
    # Add a day for Excel's missing leap day in 1900
    if xldate > 59:
        xldate += 1
    return xldate


def excel_date_1904(date):
    """Create an Excel date in the 1904 format. All of the arguments are optional
    but you should at least add years.
    
    You will also need to call workbook.set_1904() for this format to be valid.
    
    """
    epoch = datetime(1904, 1, 1, 0, 0, 0)
    delta = date - epoch
    xldate = delta.days + float(delta.seconds) / (24*60*60)
    return xldate
    
    
def main():
    """Simple example."""
    
    # Create a new workbook and add a worksheet
    workbook = xl.Writer("excel_date2.xls")
    worksheet = workbook.add_worksheet()
    
    # Expand the first column so that the date is visible.
    worksheet.set_column("A:A", 25)
    
    # Add a format for the date
    format =  workbook.add_format()
    format.set_num_format("d mmmm yyy HH:MM:SS")
    
    # Write some dates and times
    date =  excel_date(datetime(1900, 1, 1))
    worksheet.write("A1", date, format)
    
    date = excel_date(datetime(2000, 1, 1))
    worksheet.write("A2", date, format)
    
    date = excel_date(datetime(2000, 4, 17, 14, 33, 15))
    worksheet.write("A3", date, format)

    workbook.close()


if __name__ == "__main__":
    main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.