I am reading a table and writing to CSV. I have some columns where I want to change the date format. Below is some code I have
for row in out[1]:
dt = datetime.datetime.strptime(row[18], '%Y-%m-%d %H:%M:%S.%f')
row[18] = dt.strftime('%Y-%m-%d)
print(row[18])
It does seem to change the date format to what I'm looking for because the print statement shows as much, but when I open the excel sheet that it wrote, the date format is like 11/12/2019 instead of 2020-11-12.
Any advice? I'm trying to prevent manually changing the format date once opening excel
Most likely, what Excel shows you is not what the underlying data is. Excel auto formats fields when it displays them. Hence, if it opens a CSV and sees 2020-11-12 it will recognize it is a date and display it for you as a formatted date field. If you want to save a field as TEXT that Excel will know is TEXT, then the cell has to start with a " ' ". Try adding this:
row[18] = dt.strftime('\'%Y-%m-%d')
Related
Code:
def write_pandas_dataframe_to_excel(df):
book = openpyxl.load_workbook('~/Documents/test.xlsm', read_only=False, keep_vba=True)
sheet = book['Database']
# Delete all rows after the header so that we can replace them with the contents of our pandas dataframe
sheet.delete_rows(1,sheet.max_row)
#Write values from the pandas dataframe to the sheet
for r in dataframe_to_rows(df,index=include_index, header=True):
sheet.append(r)
for row in sheet[2:sheet.max_row]: # skip the header
cell = row[0] # column A is a Date Field.
cell.number_format = 'YYYY-mm-dd'
book.save(excel_file_path)
book.close()
Expected Result: I open up test.xlsm, and in column A, all dates should already be in the format YYYY-mm-dd
Actual Result: While the YYYY-mm-dd format gets applied without any issues when I run the python code, I then have to open up the excel file, select each cell manually and hit 'Return' in the formula window for the YYYY-mm-dd format to be applied.
Is there a way for my specified date format to be applied through the python code rather than having to manually apply it by opening up excel and selecting each cell, going to the formula bar and hitting 'Return' every time?
Thanks in advance!
I've figured out the answer. Put simply, the date was being written to excel as a string, and that was causing the issue.
In the pandas dataframe I'm containing my data I had used strptime to format the date, which converted the date type to a generic 'object' type. I removed the strptime so that it maintained the datetime object, and that way when I write to excel it writes as a pandas Timestamp object rather than a string.
I am trying to write dates to an excel file using Openpyxl. I am using the following lines to write the date.
dttm = datetime.datetime.strptime(ls25Dict[cell.value][2], "%m/%d/%Y" )
ws1['B'+ str(cell.row)].value = dttm
This writes the date to excel but in the wrong format. This is the output:
2018-01-09 0:00:00
I am trying to get it to be 1/9/2018. Basically change the format to Short Date in excel.
Anyone know how to change it before the date is written to excel?
In Excel you always have to provide your own format for dates and times, because these are stored as serials. openpyxl defaults to ISO formats for minimal ambiguity.
Been trying to get the set_column to work still. Having problems getting Pandas to work, so have been doing it just in xlsxwriter. Right now am using:
'worksheet.set_column('D:D',None,format4)' - this only seems to work when I go into the xlsx file and actually activate each cell in the "D" column. Is there some way of activating each cell so that I wouldn't have to do it manually?
Thanks in advance.
import xlsxwriter,os,sys,datetime
now=datetime.datetime.now()
def main():
platform=sys.platform
if platform.find('win')>=0:
TheSlash='\\'
else:
TheSlash='/'
output = '%s-%s.xlsx' % ('XlsxSample',now.strftime("%m%d%Y-%H%M"))
workbook = xlsxwriter.Workbook(output, {'strings_to_numbers':True,'default_date_format':'mm/dd/yy hh:mm'})
worksheet = workbook.add_worksheet()
count=0
counter=0
format=workbook.add_format({'font_size':'8','border':True})
formatdict={'num_format':'mm/dd/yy hh:mm'}
format4=workbook.add_format(formatdict)
cur =('Pole1','33.62283963','-90.54639967','4/20/16 11:43','-90.54640226','33.62116957','5207069','25-04','50','3','PRIMARY','PGC')
for name in cur:
worksheet.write(counter, count, name,format)
count+=1
counter+=1
worksheet.set_column('D:D',None,format4)
workbook.close()
if __name__ == "__main__":
main()
as stated above - date format only seems to activate if you get into the "D" cell itself with the cursor.
The reason that the column date format isn't showing up in the column cells is that the program is overwriting it with a cell format here:
for name in cur:
worksheet.write(counter, count, name,format)
count+=1
In XlsxWriter, as in Excel, a cell format overrides a column format.
If you want to have a cell or column format that is the result of 2 combined formats you will need to create a new format that combines those formats and apply it to the cells or the column.
Update: Also, I just noticed that you are writing a string in column D. Dates in Excel are formatted numbers. This is probably why you see the cell data change when you hit return. Excel is converting the date-like string into a formatted number displayed as a date. In XlsxWriter you will need to do the conversion. See the Working with Dates and Time section of the XlsxWriter docs.
You need change format using datetime.datetime.strptime()
Example
import datetime
datetime_result = datetime.dateime.strptime('04/20/16 11:43', '%m/%d/%Y %H:%M')
format5 = workbook.add_format({'num_format':'mm/dd/yy hh:mm'})
worksheet.write('A5', datetime_result, format5)
Refer to Working with Dates and Time in XlsxWriter docs.
In VBA, Columns("D").Select does what you want. If you are running from an external script, you might be able to save a VBA macro and run it with a technique like this: How do I call an Excel macro from Python using xlwings?.
I have a CSV file where the date is formatted as yy/mm/dd, but Excel is reading it wrongly as dd/mm/yyyy (e.g. 8th September 2015 is read as 15th of September 2008).
I know how to change the format that Excel outputs, but how can I change the format it uses to interpret the CSV data?
I'd like to keep it to Excel if possible, but I could work with a Python program.
Option 3. Import it properly
Use DATA, Get External Data, From Text and when the wizard prompts you choose the appropriate DMY combination (Step 3 of 3, Under Column data format, and Date).
Option 1. change the format excel reads in
edit: a better method is suggested in the OP comments to accomplish this, I was not aware you could do that
it(excel) uses your windows settings
so you can go to
Control Panel > Clock, Language, Region > (under Region and Language) change the date,time or number format
and enter the appropriate format
Option 2. change the csv date format
from dateutil.parser import parse
with open("output.csv","wb") as fout:
csv_out = csv.writer(fout)
for row in csv.reader(open("input.csv","rb")):
row[date_index] = parse(row[date_index]).strftime("%x")
csv_out.writerow(row)
I am trying to convert a date format in Excel to be formatted such that it can be read into a MySQL database. I am using python to process the date column, export it back into a csv file and then dump it into a MySQL table.
Here is how the date column look like :
Date
6/10/13
6/17/13
6/24/13
I want it to be in the format : 2013-06-10 or ("%Y-%m-%d")
Here is my code:
import datetime
import csv
def read(filename):
new_date=[]
cr = csv.reader(open(filename,"rU").readlines()[1:], dialect='excel')
for row in cr:
# print row[0]
cols=datetime.datetime.strptime(row[0] , "%m/%d/%y" )
newcols=cols.strftime("%Y-%m-%d")
# print newcols
new_date.append(newcols)
print new_date[0]
with open('new_file.csv', 'wb') as f:
writer = csv.writer(f)
for date in new_date:
writer.writerow([date])
The code runs, but when i open the new_file.csv, the date column automatically reverts back to the old format in excel.
How can i change this?
Thanks,
Have you tried opening the .csv in a simple text editor (like notepad, for example) to see if the date is being printed to that file correctly? My guess is that Excel, which has the ability to recognize dates, is just reformatting it to it's default date format when you open the file, and that if you open your .csv file in a simple text editor to check, you'll see that your code has correctly reformatted the dates in the csv.
(By default, Excel formats dates like you specify in Control Panel, and you can change your default date formatting in Control Panel to change Excel's default formatting.)
You can change the way that Excel formats dates as described here in the documentation.
Select the cells you want to format.
Press CTRL+1.
In the Format Cells box, click the Number tab.
In the Category list, click Date.
You can create a custom date format from this menu if the format you see is not there. Details here.