I wrote this small code. How can I remove the "hours" timing in the excel ? See the screenshot below.
import os
import yfinance
import pandas as pd
from openpyxl import Workbook
from openpyxl import load_workbook
a=yfinance.download('CS.PA', interval='1mo')
print(a)
save_name="C:\\Users\\Lex02\\Desktop\\Python2\\Bourse essai_3.xlsx"
a.to_excel(save_name)
a_excel=load_workbook(filename=save_name)
ws=a_excel.active
os.system('pause')
Thank you !
Format your index:
a = yfinance.download('CS.PA', interval='1mo')
a.index = a.index.strftime('%Y-%m-%d') # Enter whatever format here
Related
I am getting that error and have already checked the forums & have found no answer as I do have the correct imports installed. Here is my current import list:
import xlwings as xw
import xlsxwriter as xlsx
import xlrd
import xlwt
from xlutils.copy import copy
import pandas as pd
import win32com.client as win32
import openpyxl as xl
from openpyxl import load_workbook
import numpy as np
import datetime
import os.path
import warnings
Here's the code I'm getting errors on:
wb = xlsx.Workbook(File_path)
ws = ws
ws.set_column('A:A', 60)
ws comes form:
ws = wb.get_worksheet_by_name('Data')
Any help would be greatly appreciated. Thanks!
You need to replace
ws = wb.sheets['Data']
with
ws = wb.get_worksheet_by_name('Data')
I am trying to read an already existing excel file, and append some data in another sheet in that excel file. however, after closing the file, when i try to manually open it and do some manual calculation etc.. it's says that excel is read-only. can you please help me.
import numpy as np
import pandas as pd
import os, glob
import datetime as dt
import yfinance as yf
from openpyxl import load_workbook
ticker = "SBIN.NS"
start = dt.datetime.today() - dt.timedelta(5000)
end = dt.datetime.today()
ohlcv_data = pd.DataFrame()
temp_ticker = yf.Ticker(ticker)
ohlcv_data = temp_ticker.history(start=start,end=end,interval='1d')
writer = pd.ExcelWriter('D:/NSE/SBI.xlsx',engine='openpyxl',mode='a')
ohlcv_data.to_excel(writer, sheet_name='yahoo_data2')
writer.close()
Language: Python 3.8.3
I faced this error when I was importing my xlxs file ModuleNotFoundError: No module named 'xlxswriter'
import xlxswriter
import pandas as pd
from pandas import DataFrame
path = ('mypath.xlxs')
xl = pd.ExcelFile(path)
print(xl.sheet_names)
How can I fix this?
Instead of typing xlsx, type xlsx like this:
import xlsxwriter
import pandas as pd
from pandas import DataFrame
path = ('mypath.xlsx')
xl = pd.ExcelFile(path)
print(xl.sheet_names)
It'll work.
The module name is xlsxwriter not xlxswriter, so replace that line with:
import xlsxwriter
I need my code to force excel to show 3 decimals every 8th row. The dataframe is an excel file with one column. When i run my code python creates a file that is equal to the original one and i don't know why. Please help me!
This is my code:
import pandas as pd
import openpyxl
df=pd.read_excel("C:/Users/feder/Desktop/DATA.xlsx")
row=6
while IndexError==True:
value=df.iat[row,0]
new_value="{:.2f}".format(float(value))
print(new_value)
df1=df.replace(value, new_value)
row+=8
df1.to_excel("angelquirroz2.xlsx")
book= openpyxl.load_workbook("angelquirroz2.xlsx")
sheet=book["Sheet1"]
book.save("angelquirroz2.xlsx")
print("done")
now i managed to write this loop, ma it takes more than 8 hours to modify 1000 files and i don't think i made it really efficient.
import pandas as pd
import openpyxl
import os
import xlsxwriter
from pandas import ExcelWriter
from os import path
import shutil
directory_path= r"C:\Users\..."
row=6
for filename in os.listdir(directory_path):
if filename.endswith("CHANGED.xlsx"):
continue
if filename.endswith(".ipynb"):
continue
elif filename.endswith("checkpoints"):
continue
elif filename.endswith("spyproject"):
continue
else:
workbook=openpyxl.load_workbook(filename)
df=pd.read_excel(filename)
lenght=len(df)
row=6
while row<=lenght:
value=df.iat[row,0]
new_value=df.iat[row,0]="{:.3f}".format(float(value))
row+=8
#print(new_value)
#print("done")
file=filename.replace(".xlsx","")
df.to_excel(""+file+"CHANGED.xlsx")
print ("DONE!Now let's move the files in a new directory")
directory_output=input("insert the directory path here: ")
dst= ""r""+directory_output+""
for filename in os.listdir(directory_path):
if filename.endswith("CHANGED.xlsx"):
shutil.copy(path.join(directory_path, filename), dst)
else:
continue
thanks for the help
Struggling to get xlxswriter to create multiple worksheets and use the pandas df.to_excel method to place the data in there.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import xlsxwriter
import os as os
import sqlite3 as sql
import xlrd
path = (r'C:\Users\test1\Downloads\category.xlsx') #folder
data = pd.DataFrame()
data = pd.read_excel(path,sheetname='1')
print (data.shape)
data.head()
#create excel workbook using xlsxwriter
#workbook = xlsxwriter.Workbook('hyperlink.xlsx')
writer = ExcelWriter('hyperlink.xlsx')
data.to_excel(writer,'Sheet1')
workbook.close()