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

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:

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)

ExcelWriter align center error when opening output file

This code is working well:
excel_path = os.path.join(path,'Report For Yr '+str(date.today().year)+' Mth ' +mth+'.xlsx')
writer = pd.ExcelWriter(excel_path, engine='xlsxwriter')
df2.to_excel(writer, sheet_name='report')
workbook = writer.book
worksheet = writer.sheets['report']
format1 = workbook.add_format({'num_format': '#,###'})
format2 = workbook.add_format({'num_format': '0.00%'})
format3 = workbook.add_format({'num_format': '#,##0.00'})
worksheet.set_column('B:I', 10, format1)
worksheet.set_column('J:J', 10, format2)
worksheet.set_column('L:L', 10, format2)
worksheet.set_column('K:K', 10, format3)
writer.save()
I'm trying to align center for certain columns. The code ran without any error but there is an error when opening the excel file:
Excel cannot open the file because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
I tried with the working code and change this:
format1 = workbook.add_format({'num_format': '#,###'},{'align': 'center'})
got the error message
I tried with the working code and change this:
worksheet.set_column('B:I', 10, format1,{'align': 'center'})
also got the error message
Anyone can help?
I tried with the working code and change this:
That isn't the correct syntax. You should pass a single dict of values. Like this:
format1 = workbook.add_format({'num_format': '#,###', 'align': 'center'})

Saving a Excel file with current date in python3

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)

How do i use more than one delimiter?

So, i'm trying to create a way to convert a tsv file to csv, but already with tab "\t" and delimiter= ' ',
there is a way of doing that?
from xlsxwriter.workbook import Workbook
tsv_file = '1.tsv'
xlsx_file = '1.xlsx'
workbook = Workbook(xlsx_file)
worksheet = workbook.add_worksheet()
tsv_reader = csv.reader(open(tsv_file,'rt'),delimiter="\t")
for row, data in enumerate(tsv_reader):
worksheet.write_row(row, 0, data)
workbook.close()`

How can I iterate over worksheets in win32com?

I generate an xlsx file with lots of sheets and I want to take me at specific position when I open it manually with Excel. This function does the job but for one sheet only. How can I apply it to all of the sheets in workbook?
import win32com.client
def select_cell():
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(r'H:\Files\1.xlsx')
ws = xl.ActiveSheet
ws.Range('B100').Select()
wb.Close(True)
xl.Quit()
select_cell()
I want to make something like this:
import win32com.client
def select_cell():
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(r'H:\Files\1.xlsx')
for ws in wb.Worksheets():
ws.Range('B100').Select()
wb.Close(True)
xl.Quit()
select_cell()
In order to be taken to specific cell in newly generated document it is necessary to have both of these expressions executed:
ws.Range('k100').Value = 1
ws.Range('k100').Select()
To do it in every sheet of the workbook:
def select_cell():
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(r'H:\Files1.xlsx')
for sh in wb.Sheets:
xl.Worksheets(sh.Name).Activate()
ws = xl.ActiveSheet
ws.Range('k100').Value = 1
ws.Range('k100').Select()
wb.Close(True)
xl.Quit()
The code above will take you to K100 on every worksheet in the book.
Your script did nothing at all when I tested it.
The script below worked fine, based on my test.
import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('C:\\Users\\Excel\\Desktop\\book1.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some simple text.
worksheet.write('A1', 'Hello')
# Text with formatting.
worksheet.write('A2', 'World', bold)
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
workbook.close()

Categories