Question about formatting data when writing to an Excel doc as I am a bit newer to using Openpyxl.
I have an excel sheet that I am writing data to where one of the columns is a column that holds the current date in 'mm/dd/yyyy' format. Currently when writing to the Excel doc, my code reformats the date to 'yyyy-mm-dd' format, and the excel doc does not recognize the data as 'Date' type, but as 'General' data type.
Here is my Python code to write the date to the sheet.
from openpyxl import Workbook
from openpyxl import load_workbook
from date time import date
workbookName = "Excel workbook.xlsm"
wb = Workbook()
wb = load_workbook(workbookName, data_only=True, keep_vba=True)
ws = wb["Sheet1"]
rowCount = 2000
insertRow = rowCount + 7
origDate = date.today()
dateString = datetime.datetime.strftime(origDate, '%m/%d/%Y')
insertDate = datetime.datetime.strptime(dateString, '%m/%d/%Y').date()
dateCell = ws.cell(row = insertRow, column = 1)
dateCell.value = insertDate
wb.save("Excel workbook.xlsm")
So for example, if I ran this code using today's date of 03/19/2021
, the cell would look like 2021-03-18
with General
type.
Not sure what I am missing, but I want the inserted cell to have 'Date' type in 'mm/dd/yyyy' format. Any pointers?