I have an excel file that consists of hourly basis values, date and time as timeslot.
I want to import this excel to PostgreSQL DB, but I need to edit the date and time before. Otherwise its giving error.
Editing that date and time in excel also okay but I think doing it in python would be much easier.
I want to transform that date and time into this timeslot:
2018-05-27 00:00:00
I solve it like this, and it worked. Someone may wonder about the solution.
import pandas as pd
from xlwt import Workbook
from datetime import datetime
df = pd.read_excel('yourexcell file.xlsx')
date=[]
for i in df.index:
start_date = df['date'][i] #date column name in excel file
start_time=df['time'][i] #time column name in excel file
date_time = datetime.strptime(start_date, "%d.%m.%Y")
comb=datetime.combine(date_time,start_time)
date.append(comb)
#dateandtime combined and ready.
wb = Workbook()
sheet = wb.add_sheet('Sheet 1')
for k in range(len(date)):
sheet.write(k,0,date[k])
wb.save('datetime_generated'+".CSV")
print("csv file saved.")
and here it is:
output
2018-05-27 00:00:00
Related
I'm new to python and would really appreciate some assistance. I exported the SSMS query to pandas data frame using Jupyter Notebook and then exported the data frame to csv. I need to run this python script every month so I am looking for the monthly csv file to be saved in a certain format i.e. CSV file name + Previous Month Name & Year eg: CSV File Name February 2022 when the python script is run in March 2022.
The problem is the Sheet Name of that CSV file shows CSV File Name along with Month Name and Year and I would like the Sheet Name to remain as "Sheet1" and not change every month when the python script is run i.e. when I run the script in April 2022 I do not want the Sheet Name to become CSV File Name March 2022 and instead remain as "Sheet1"
Here's my code:
import pyodbc
import pandas as pd
import openpyxl
from datetime import date
from dateutil.relativedelta import relativedelta
last_month = date.today() - relativedelta(months=1)
text_date = format(last_month, '%B %Y')
server = 'server_name'
database = 'database_name'
cnxn = pyodbc.connect('DRIVER= {SQL Server};\
SERVER=' + server + '; \
DATABASE=' + database + ';\
Trusted_Connection = yes;')
sql_query = pd.read_sql_query("""Select * from Table""", cnxn)
df = pd.DataFrame(sql_query)
df.to_csv(r'filepath \filename' + str(text_date) + '.csv', index = False)
Kindly help me on this. Thank you.
I don't think you can have sheet_name option with df.to_csv, try using df.to_excel
df.to_excel(filename, sheet_name="sheet_name", index=False)
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?
I think this can be done simply with:
dateCell.value = origDate
dateCell.number_format = 'mm/dd/yyyy'
Note that there's no such thing as a date data type in Excel. A date is just a formatted number. "Date" (and "General" for that matter) are just formatting; the underlying value of the cell is separate and unchanged.
Though if you really want the cell to show as having a "Date" format and not "Custom", perhaps:
dateCell.number_format = 'mm/dd/yyyy;#'
Convert date string "1/09/2020" to string "1-Sep-2020" in python. Try every solution mentioned in stackoverflow but not able to change it. Sometimes the Value error comes data format doesn't match, when I try to match it then error come day out of range. Is there problem in excel data or I am writing the code wrong. Please help me to solve this issue???
xlsm_files=['202009 - September - Diamond Plod Day & Night MKY021.xlsm']
import time
import pandas as pd
import numpy as np
import datetime
df=pd.DataFrame()
for fn in xlsm_files:
all_dfs=pd.read_excel(fn, sheet_name=None, engine='openpyxl')
list_data = all_dfs.keys()
all_dfs.pop('Date',None)
all_dfs.pop('Ops Report',None)
all_dfs.pop('Fuel Report',None)
all_dfs.pop('Bit Report',None)
all_dfs.pop('Plod Example',None)
all_dfs.pop('Plod Definitions',None)
all_dfs.pop('Consumables',None)
df2 = pd.DataFrame(columns=["PlodDate"])
for ws in list_data:
df1 = all_dfs[ws]
new_row = {'PlodDate':df1.iloc[3,3]}
df2 = df2.append(new_row,ignore_index=True)
df2['PlodDate']=pd.to_datetime(df2['PlodDate'].astype(str), format="%d/%m/%Y")
df2['PlodDate']=df2['PlodDate'].apply(lambda x: x.strftime("%d-%b-%Y"))
df2
ValueError: day is out of range for month or doesnot match format
Method 1-Tried because it show error date out of range
try:
datetime.datetime.strptime(df2['PlodDate'].astype(str).values[0],"%d/%m/%Y")
except ValueError:
continue
df2['PlodDate']=pd.to_datetime(df2['PlodDate'].astype(str), format="%d/%m/%Y")
df2['PlodDate']=df2['PlodDate'].apply(lambda x: x.strftime("%d-%b-%Y"))
Excel File Attached
df2['PlodDate']=pd.to_datetime(df2['PlodDate'].astype(str), format="%d/%m/%Y")
date = df2['PlodDate'].split('/')
df2['PlodDate'] = datetime.date(int(date[2]), int(date[1]), int(date[0])).strftime('%d-%b-%Y')
How do I properly define a date column?
I'm reading a csv file which contains a date column, and writing that out to an excel file.
Using pd.to_datetime I can get the values represented as datetime. However as my input is date only, the time values come out as zeroes.
I've tried using format='%d/%m/%Y however that fails as my input is not zero padded for the day and month values –
ValueError: time data '07/18/2016' does not match format '%d/%m/%Y' (match)
import pandas as pd
from datetime import datetime, date
jobs_file = "test01.csv"
output_file = jobs_file.replace('csv', 'xlsx')
jobs_path = "C:\\Users\\Downloads\\" + jobs_file
output_path = "C:\\Users\\Downloads\\" + output_file
jobs = pd.read_csv(jobs_path,)
jobs['LAST RUN'] = pd.to_datetime(jobs['LAST RUN'])
#jobs['LAST RUN'] = pd.to_datetime(jobs['LAST RUN'], format='%d/%m/%Y')
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(output_path, engine='xlsxwriter',
date_format='mm/dd/yyyy')
# Convert the dataframe to an XlsxWriter Excel object.
jobs.to_excel(writer, sheet_name='Jobs', index=False)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
My input values look like this -
7/18/2016
Output is looking like this -
7/18/2016 12:00:00 AM
I'd like it to be just the date -
7/18/2016
Python newbie here! :)
Basically, I am trying to scan an excel file's column A (which contains all dates) and if the date in the cell is 7 days in the future...do something. Since I am learning, I am just looking at one cell before I progress and start looping through the data.
Here is my current code which isn't working.
import openpyxl, smtplib, datetime, xlrd
from openpyxl import load_workbook
from datetime import datetime
wb = load_workbook(filename = 'FRANKLIN.xlsx')
sheet = wb.get_sheet_by_name('Master')
msg = 'Subject: %s\n%s' % ("Shift Reminder", "Dear a rem ")
cell = sheet['j7'].value
if xlrd.xldate_as_tuple(cell.datemode) == datetime.today.date() + 7:
print('ok!')
Here is the error code I am getting: 'datetime.datetime' object has no attribute 'datemode'
I've tried searching high and low, but can't quite find the solution.
Your cell variable seems to be datetime.datetime object. So you can compare it like this:
from datetime import timedelta
if cell.date() == (datetime.now().date() + timedelta(days=7)):
print("ok")