Unable to format date in 2.4.0

Issue #695 resolved
vijay-axis
created an issue

Unable to format date to this format 'D MMM YY'

cell = WriteOnlyCell(ws)
cell.number_format = 'D MMM YY'
cell.value = datetime.datetime.now()

format is not changing at all. It is showing the date in the default format

Comments (7)

  1. vijay-axis reporter
    
    

    from datetime import datetime
    from openpyxl.cell.cell import WriteOnlyCell

    from openpyxl import Workbook

    wb = Workbook()
    ws = wb.active

    def style_date(row):
    for i, val in enumerate(row):
    cell = WriteOnlyCell(ws)
    if i == 0:
    cell.number_format = 'D MMM YY'
    yield cell

    if name == 'main':
    row = [
    datetime.datetime(2016, 10, 15, 0, 0),
    778, 644, 584, 36, 191, 713, 280, 26,
    25, 54, 846, 212, 7, 207, 73, 331, 96, 226, 527, 61]
    ws.append(style_date(row))
    wb.save("file.xlsx")

  2. Laurent LAPORTE

    This is not a bug, you only forget to set the value to the cell, the val variable is not used.

    Replace:

    def style_date(row):
        for i, val in enumerate(row):
            cell = WriteOnlyCell(ws)
            if i == 0:
                cell.number_format = 'D MMM YY'
            yield cell
    

    by:

    def style_date(row):
        for i, val in enumerate(row):
            cell = WriteOnlyCell(ws, val)
            if i == 0:
                cell.number_format = 'D MMM YY'
            yield cell
    
  3. vijay-axis reporter

    Thank you this resolved the issue. I was asssigning value this way
    cell.value = val and it was not working. passing value to WriteOnlyCell worked

  4. Log in to comment