How can I correctly format dates using openpyxl?
<p>I am attempting to save dates in an existing Excel workbook using openpyxl but they are not formatted correctly in the cells when I open the workbook.</p>

<p>The dates are formatted in a list as such: <code>mylist = ["09/01/2016","01/29/2016"]</code></p>

<p>The cells in which dates are being saved are formatted in the workbook as Date "*m/d/yyyy". When viewing the workbook, the dates are normally displayed in this format: 9/1/2016  1/29/2016</p>

<p>Using this code in Python:</p>

<pre><code>SheetName.cell(row=2,column=1).value = mylist[0]
</code></pre>

<p>inserts the date formatted as 09/01/2016 and appears to be formatted as text (left justified).</p>

<p>If you click on the cell containing the date, then click on the formula bar, then press Enter, Excel formats and displays the date as 9/1/2016 (right justified).</p>

<p>I could spend time rewriting the code to strip leading zeros out of the month and day but this wouldn't address the formatting issue in Excel.</p>

<p>Is there a way to control the formatting of the cell when the date is added to a cell?</p>

<p>openpyxl supports <a href="https://docs.python.org/2/library/datetime.html#datetime-objects" rel="nofollow"><code>datetime</code></a> objects, which convieniently provide a way to convert from a string. </p>

<pre class="lang-python prettyprint-override"><code>import datetime
for i in len(mylist):
    dttm = datetime.datetime.strptime(mylist[i], "%m/%d/%Y")
    SheetName.cell(row=i,column=1).value = dttm
</code></pre>

python openpyxl