Am .06.2016, 12:23 Uhr, schrieb Stefan <
stefan...@gmail.com>:
> Hi,
>
> I need use negative times in Excel and therefore need Excel's "Use 1904
> date format" option. So far, I was not able to write a file from openpyxl
> with this option enabled.
Sounds interesting. Can you please provide some more information? The
specification has the following to say about the two different epochs:
"""
Two different bases are used for converting dates to and from serial
date-times:
In the 1900 date system, the lower limit is January 1st, 0001 00:00:00,
which has a serial date-
time of -693593. The upper-limit is December 31st, 9999, 23:59:59.999,
which has a serial date-time of 2,958,465.9999884. The base date for this
system is 00:00:00 on December 30th, 1899, which has a serial date-time of
0.
In the 1904 date system, the lower limit is January 1st, 0001, 00:00:00,
which has a serial date-time
of -695055. The upper limit is December 31st, 9999, 23:59:59.999, which
has a serial date-time of 2,957,003.9999884. The base date for this system
is 00:00:00 on January 1st, 1904, which has a serial date-time of 0.
"""
In practice, Excel can't handle negative date times, but you should be
able to get them with either epoch. The 1904 is basically a wart from an
older version of Excel for MacOS that I'd like to remove, because this
version has no been supported for a while, we also can't test with it and
it would vastly simplify the handling of dates and times. Can you provide
a file showing this in practice?
> One idea I had was to create a blank file in Excel, enable said option
> and save it. Then I use load_workbook to load this file, write my stuff
> into it and save it again. If you try to load a file with the 1904 date
> format
> enabled, you get an error:
>
> Traceback (most recent call last):
> File "./[...].py", line 179, in <module>
> wb3 = load_workbook('[...]')
> File "[...]/src/openpyxl/openpyxl/reader/excel.py", line 152, in
> load_workbook
> parser.parse()
> File "[...]/src/openpyxl/openpyxl/packaging/workbook.py", line 46, in
> parse
> wb.excel_base_date = CALENDAR_MAC_1904
> NameError: global name 'wb' is not defined
If you're seeing the workbook package then I assume you're working with
2.4?
> This was easily fixed by replacing
> wb.excel_base_date = CALENDAR_MAC_1904
> in the workbook.py on line 46 with
> self.wb.excel_base_date = CALENDAR_MAC_1904
Looks good. I haven't got time at the moment to fix this including the
relevant test. Do you fancy submitting a PR for this?
> But now that I can load the file, the 1904 option still gets removed
> after saving again. Is it possible to save files from openpyxl with the
> 1904 date format option enabled?
See above: I don't think this should strictly be necessary and that
normalising files to the 1900 epoch is the way to go but I guess it should
be available if it is really required?
Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel:
+49-211-600-3657
Mobile:
+49-178-782-6226