Saving a Excel file with current date in python3 - python

I would like to save the excel file(File Name- Stock) in python along with current date, let me know how to save it with date.
Below is the Script am using normally to save the excel files.
path=r"\C:\User\ASL - Stock Reports\Stock.xlsx"
writer=pd.ExcelWriter(path,engine='xlsxwriter')
Overall_Stock.to_excel(writer, index=False)
writer.save()
writer.close()
Thanks in advance

import datetime
import pandas as pd
now = datetime.datetime.now()
date = '{}-{}-{}'.format(now.year, now.month, now.day)
filename = 'Name_of_your_file' + '_' + date
path=r"\C:\User\ASL - Stock Reports\Stock.xlsx"
writer=pd.ExcelWriter(path, sheet_name = filename, engine='xlsxwriter')
Overall_Stock.to_excel(writer, index=False)
writer.save()
writer.close()
This should work. Let me know if it does not.

Something like this:
from datetime import datetime
curr_date = datetime.strftime(datetime.now(), '%Y_%m_%d')
Overall_Stock.to_excel(path.split('.xlsx')[0] + '_' + curr_date + '.xlsx', index=False)

Related

ValueError: Sheet 'Sheet1' already exists and if_sheet_exists is set to 'error'

I am trying to create an excel file of 3 columns: System Date, Time, Value on a webpage at that time.
Intention is to create a dataframe of the 3 values, every time the code runs, and append the dataframe to existing excel workbook (with one existing sheet).
I am able to create dataframe every time code runs, but when I try to append it to an excel file, it throws error:
ValueError: Sheet 'Sheet1' already exists and if_sheet_exists is set to 'error'
Can you please suggest, where am I going wrong.
# Importing Libraries
from datetime import datetime
import pandas as pd
import requests
from bs4 import BeautifulSoup
import openpyxl
#getting today's date amd formatting it
now = datetime.now()
Date = now.strftime ("%d/%m/%Y")
Time = now.strftime ("%H:%M")
# GET request to scrape. 'Page' variable to assign contents
page = requests.get("https://www.traderscockpit.com/?pageView=live-nse-advance-decline-ratio-chart")
# Create BeautifulSoup object to parse content
soup = BeautifulSoup(page.content, 'html.parser')
adv = soup.select_one('a:-soup-contains("Advanced:")').next_sibling.strip()
dec = soup.select_one('a:-soup-contains("Declined:")').next_sibling.strip()
ADratio = round(int(adv)/int(dec), 2)
df = pd.DataFrame({tuple([Date, Time, ADratio])})
#Load workbook and read last used row
path = r'C:\Users\kashk\OneDrive\Documents\ADratios.xlsx'
writer = pd.ExcelWriter (path, engine='openpyxl', mode = 'a')
wb = openpyxl.load_workbook(path)
startrow = writer.sheets['Sheet1'].max_row
#Append data frame to existing table in existing sheet
df.to_excel (writer, sheet_name = 'Sheet1', index = False, header = False, startrow = startrow)
writer.save()
writer.close()
A fast and easy solution would be upgrading your pandas > 1.4.0 since it provides a if_sheet_exists = 'overlay' Source
pd.ExcelWriter(path, engine='openpyxl', mode='a', if_sheet_exists='overlay')
If you don't want to upgrade your pandas, there is a way to work around by removing and re-write the sheet into the excel file. (Not recommended if you have a lot of records since it will be slow).
path, sheet_name = 'ADratios.xlsx' , 'Sheet 1'
df.columns = ['Date','Time','ADratio']
with pd.ExcelWriter(path, engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
book = openpyxl.load_workbook(path, 'r')
df_bak = pd.read_excel(path)
writer.book = openpyxl.load_workbook(path)
writer.book.remove(writer.book.worksheets[writer.book.sheetnames.index(sheet_name)])
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
pd.concat([df_bak, df], axis=0).to_excel(writer, sheet_name=sheet_name, index = False)

How to make cell format of 'MMM DD' in xlswriter

I'm stuck in this problem of formatting a cell to date in xlswriter.
The thing is the date is 07/02/2021 and the below image is the result of a manual cell format in excel file which is the correct thing.
While these are the codes I wrote to imitate it.
import xlsxwriter
from datetime import date, datetime
def Test():
workbook = xlsxwriter.Workbook(path)
worksheet = workbook.add_worksheet()
date_string = "2021-07-02"
datex = datetime.fromisoformat(date_string)
number = datetime.timestamp(datex)
formatx = workbook.add_format({'num_format': 'MMM DD'})
worksheet.write('A1', number, formatx)
workbook.close()
But the result is not even close:
I wanted to imitate a format of 'MMM DD' in excel file that if you look at the 'VALUEs BAR' it is '07/02/2021' and if you look at the 'EXCEL ROW DATA' it is 'July 02'.
But the codes I wrote have the result of these:
'VALUEs BAR': '10/22/ -5025'
'EXCEL ROW DATA': 'Oct 22'
When I clearly set the right date in the variable 'date_string'. How can I do this correctly?
The issue is with the line number = datetime.timestamp(datex), remove it:
import xlsxwriter
from datetime import date, datetime
def Test():
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
date_string = "2021-07-02"
datex = datetime.fromisoformat(date_string)
formatx = workbook.add_format({'num_format': 'MMM DD'})
worksheet.write('A1', datex, formatx)
workbook.close()
Output:

Saving a csv with current date and time

I am running some code and I would like to save a csv file which include the current date and time in its name.
For example: I run some code now (12:24, Jan 15) and I would like to have something like
name_1224_01152021.csv
Can you tell me how to print/save this information, please?
The following code should format the name as per your requirement:
import datetime
name = f'name_{datetime.datetime.now().strftime("%H%M_%m%d%Y")}.csv'
print(name)
# prints 'name_0628_01152021.csv'
Here is the code according to your question :
from datetime import datetime
filename = datetime.now().strftime('filename_%H%M_%m%d%Y.csv')
with open(filename, "w+") as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(["row1", "row2"])
in filename you have to write your file name the output of this will be shown as
filename_0620_01152021_.csv
Something like this might be what you want :
from datetime import date
today = date.today()
# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
fname = "name_1224" + d1 + ".csv"
#fname = "name_1224" + str(d1) + ".csv"
import datetime
val_time =datetime.time.now()
now you can append name and val_time and save file with that name
You can format your date with strftime.
import datetime
filename = datetime.datetime.now().strftime('name_%H%_%d%m%Y.csv')
then you can do something like
open(filename, "w").write("blahblah")

Delete row from text file if date is older than 24 hours

I have a text file called temp.txt and I want to delete all rows in it if the date is older than 24 hours from 21:45pm everyday. I've done a lot of googling and can't find the answer anywhere. The text file is in this format with no headers:
http://clipsexample1.com,clips1,clipexample123,2019-03-28 17:14:14
http://clipsexample12com,clips2,clipexample234,2019-03-27 18:56:20
Is there anyway I could remove the whole row if it is older than 24 hours (the second clip in the example)
EDIT: I have tried using this code but that's just removing todays date, how do I get it to remove today-24 hours?
save_path = 'clips/'
completeName = os.path.join(save_path, 'clips'+str(today)+'.txt')
good_dates = [str(today)]
with open('temp.txt') as oldfile, open(completeName, 'w') as newfile:
for line in oldfile:
if any(good_date in line for good_date in good_dates):
newfile.write(line)
EDIT 30/03/2019: Here is my full code to try and understand how the timestamp field is created:
#change UNIX to standard date format
def get_date(created_utc):
return dt.datetime.fromtimestamp(created_utc)
_timestamp = topics_data["created_utc"].apply(get_date)
topics_data = topics_data.assign(timestamp = _timestamp)
timestamp = _timestamp
print(timestamp)
#remove UNIX data column
topics_data.drop('created_utc', axis=1, inplace=True)
#export clips to temp.txt
topics_data.to_csv('temp.txt', header=True, index=False)
import csv
from datetime import datetime, timedelta
import os
today = datetime.today()
cutoff = datetime(year=today.year, month=today.month, day=today.day,
hour=21, minute=45)
max_time_diff = timedelta(hours=24)
input_file = 'temp.txt'
save_path = './clips'
complete_name = os.path.join(save_path, 'clips'+today.strftime('%Y-%m-%d')+'.txt')
os.makedirs(save_path, exist_ok=True) # Make sure dest directory exists.
with open(input_file, newline='') as oldfile, \
open(complete_name, 'w', newline='') as newfile:
reader = csv.reader(oldfile)
writer = csv.writer(newfile)
for line in reader:
line_date = datetime.strptime(line[3], "%Y-%m-%d %H:%M:%S")
if cutoff - line_date < max_time_diff:
writer.writerow(line)
When I print the timestamp field, this is the result i get:
01 2019-03-29 01:22:09
02 2019-03-29 02:42:21
03 2019-03-28 17:14:14
04 2019-03-29 06:06:18
Name: created_utc, dtype: datetime64[ns]
And the error I am still getting is:
ValueError: time data 'timestamp' does not match format '%Y-%m-%d %H:%M:%S'
Even though the datetime is printing in that format?
Here's how to do it using the csv module as I suggested in a comment:
import csv
from datetime import datetime, timedelta
import os
today = datetime.today()
cutoff = datetime(year=today.year, month=today.month, day=today.day,
hour=21, minute=45)
max_time_diff = timedelta(hours=24)
input_file = 'date_temp.txt'
save_path = './clips'
complete_name = os.path.join(save_path, 'clips'+today.strftime('%Y-%m-%d')+'.txt')
os.makedirs(save_path, exist_ok=True) # Make sure dest directory exists.
with open(input_file, newline='') as oldfile, \
open(complete_name, 'w', newline='') as newfile:
reader = csv.reader(oldfile)
writer = csv.writer(newfile)
next(reader) # Skip header.
for line in reader:
line_date = datetime.strptime(line[3], "%Y-%m-%d %H:%M:%S")
if cutoff - line_date < max_time_diff:
writer.writerow(line)
print('done')

How to get Python to add the current time and date automatically as the filename?

I am trying to get python to automatically add the current time and date as a filename after it finishes with running the report its designed to.
Get current date from datetime module using datetime.date.today(), use strftime() to parse it to your required format and add it to your filename string.
import datetime
filename = 'XYZ Report {0}.txt'
current_date = datetime.date.today().strftime('%d %b %Y')
filename = filename.format(current_date)
# filename = XYZ Report 19 Sep 2018.txt
with open(filename) as file_obj:
# File writing logic
Answer is:
import time
import xlwt
import csv
date_string = time.strftime("%Y%m%d %H.%M")
if saving to CSV document
data_output = []
with open('C:\\Users\\Desktop\\File Name' + date_string + '.csv', 'a+') as f:
w = csv.writer(f, delimiter=',')
for data in data_output:
w.writerow(data)
f.close()
if saving to Excel document
df.to_excel('C:\\Users\\Desktop\\File Name' + date_string + '.xlsx')
if saving to Txt document
df.to_csv('C:\\Users\\Desktop\\File Name' + date_string + '.txt')

Categories