When I run this code, the excel sheet is updated with values but I am getting the following error messages. Please help me.
import glob
from operator import index
import pandas as pd
import numpy as np
import xlwings as xw
master_file = Master.xlsm'
ytd_file = ytd_sales.xlsx'
df1 =pd.read_excel(master_file, sheet_name='Cap', header=52)
df2 = pd.read_excel(ytd_file, sheet_name= 'Sub App', header=2)
df_master_subApp = df1.head(106)
df_ytd_subApp = df2.head(55)
wb1 = xw.Book(master_file)
ws1 = wb1.sheets['Cap']
df_subApp = pd.merge(df_master_subApp, df_ytd_subApp[['Item no.', 'NS']], on='Item no.', how='left')
df_subApp["January"] = df_subApp["NS"] # This will copy the data from 'Amount' column to 'JAN' column
df_subApp.drop("NS", axis=1, inplace=True)
ws1.range('L53:L159').options(transpose = False, index=False, numbers=int).value = df_subApp["January"]
Error Messages:
File "c:\Users\%usename%\Documents\PythonAutomation\FinMonEndReportAutomate\merge_fin_master_ytd_file.py", line 75, in <module>
ws1.range('L53:L159').options(transpose = False, index=False, numbers=int).value = df_subApp["January"].values # This code will fill the "January" column in master sheet
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\main.py", line 2052, in value
conversion.write(data, self, self._options)
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\conversion\__init__.py", line 48, in write
pipeline(ctx)
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\conversion\framework.py", line 66, in __call__
stage(*args, **kwargs)
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\conversion\standard.py", line 74, in __call__
self._write_value(ctx.range, ctx.value, scalar)
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\conversion\standard.py", line 62, in _write_value
rng.raw_value = value
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\main.py", line 1645, in raw_value
self.impl.raw_value = data
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\_xlwindows.py", line 859, in raw_value
self.xl.Value = data
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xlwings\_xlwindows.py", line 109, in __setattr__
return setattr(self._inner, key, value)
File "C:\Users\%usename%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\win32com\client\__init__.py", line 595, in __setattr__
self._oleobj_.Invoke(*(args + (value,) + defArgs))
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)
Related
I have two folders, 1 and 2. I want to go to each folder which has the file Test.xlsx. I tried to iterate on file_loc using i in range(1,3) but there's an error. The code works if I mention 1 or 2 on file_loc.
import pandas as pd
import numpy as np
for i in range(1,3):
file_loc = "C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\i\\Test.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
A=df["N"].to_numpy()
print([A])
A = [x for x in A if str(x) != 'nan']
print(A)
A = [eval(e) for e in A]
print(A)
A=np.array(A)
print([A])
A_mean=[]
for i in range(0,len(A)):
A_mean.append(np.mean(A[i]))
print(*A_mean, sep='\n')
The error is
Traceback (most recent call last):
File "C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\Test.py", line 12, in <module>
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
File "C:\Users\USER\anaconda3\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\USER\anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 364, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "C:\Users\USER\anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1191, in __init__
ext = inspect_excel_format(
File "C:\Users\USER\anaconda3\lib\site-packages\pandas\io\excel\_base.py", line 1070, in inspect_excel_format
with get_handle(
File "C:\Users\USER\anaconda3\lib\site-packages\pandas\io\common.py", line 711, in get_handle
handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\i\\Test.xlsx'
for i in range(1,3):
file_loc = f"C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\{i}\\Test.xlsx"
...
Make sure you entered correct path
I have an example from the pandas documentation site and can't get it run. Export as excel file works well, but the following import not:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()
df.to_excel("/tmp/foo.xlsx", sheet_name="Sheet1")
print("Reading data back from an excel file")
df2=pd.read_excel("/tmp/foo.xlsx", "Sheet1", index_col=None, na_values=["NA"])
#print(df2)
my error message:
python3 /tmp/downloads/tmp_358/main.py
Reading data back from an excel file
Traceback (most recent call last):
File "/tmp/downloads/tmp_358/main.py", line 10, in <module>
df2=pd.read_excel("/tmp/foo.xlsx", "Sheet1", index_col=None, na_values=["NA"])
File "/usr/local/lib/python3.6/dist-packages/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/excel/_base.py", line 353, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
return open_workbook(filepath_or_buffer)
File "/usr/local/lib/python3.6/dist-packages/xlrd/__init__.py", line 170, in open_workbook
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported
Python 3.6.9 and pandas==1.1.4 on Ubuntu 18.04
Can you try this:
df.to_excel("/tmp/foo.xlsx", sheet_name="Sheet1", engine='openpyxl')
df2=pd.read_excel("/tmp/foo.xlsx", "Sheet1", index_col=None, na_values=["NA"], engine='openpyxl')
The code gives an error saying KeyError in the end. But I believe the code is correct as written by the tutor.
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
def process_workbook(filename):
wb = xl.load_workbook(filename)
sheet = wb['Sheet1']
for row in range(2,sheet.max_row + 1):
cell = sheet.cell(row, 3)
corrected_price = cell.value * 0.9
corrected_price_cell = sheet.cell(row,4)
corrected_price_cell.value = corrected_price
values = Reference(sheet,
min_row = 2 ,
max_row = sheet.max_row,
min_col = 4,
max_col = 4)
chart = BarChart() #instance of BarChart class
chart.add_data(values)
sheet.add_chart(chart, 'e2')
wb.save(filename)
filename = 'transactions.xlsx'
work = process_workbook(filename)
C:\Users\bhargav\PycharmProjects\SelfLearning\venv\Scripts\python.exe
C:/Users/bhargav/PycharmProjects/SelfLearning/app.py
Traceback (most recent call last): File
"C:/Users/bhargav/PycharmProjects/SelfLearning/app.py", line 26, in
work = process_workbook(filename)
File "C:/Users/bhargav/PycharmProjects/SelfLearning/app.py", line 5,
in process_workbook
wb = xl.load_workbook(filename)
File
"C:\Users\bhargav\PycharmProjects\SelfLearning\venv\lib\site-packages\openpyxl\reader\excel.py",
line 312, in load_workbook
reader.read()
File
"C:\Users\bhargav\PycharmProjects\SelfLearning\venv\lib\site-packages\openpyxl\reader\excel.py",
line 268, in read
self.read_manifest()
File
"C:\Users\bhargav\PycharmProjects\SelfLearning\venv\lib\site-packages\openpyxl\reader\excel.py",
line 136, in read_manifest
src = self.archive.read(ARC_CONTENT_TYPES)
File
"C:\Users\bhargav\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py",
line 1428, in read
with self.open(name, "r", pwd) as fp:
File
"C:\Users\bhargav\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py",
line 1467, in open
zinfo = self.getinfo(name)
File
"C:\Users\bhargav\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py",
line 1395, in getinfo
'There is no item named %r in the archive' % name) KeyError: "There is no item named '[Content_Types].xml' in the archive"
Process finished with exit code 1
Check if the file 'transactions.xlsx' is in the same folder.
The file transactions.xlsx is not updated and this is precisely due to add_chart(). If I comment out add_chart all these errors does not arise but I want to draw chart and this is the only method I know. transanctions2.xlsx file is created but it is corrupted whereas bunch of errors do come up.
from openpyxl.chart import BarChart, Reference
wb1 = xl.load_workbook('transactions.xlsx')
sheet = wb1['Sheet1']
cell = sheet.cell(1, 1)
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 3)
corrected_price = cell.value * 0.9
corrected_price_cell = sheet.cell(row, 4)
corrected_price_cell.value = corrected_price
values = Reference(sheet,
min_row=2,
max_row=sheet.max_row,
min_col=4,
max_col=4)
chart = BarChart()
chart.add_data(values)
sheet.add_chart(chart, 'f2')
wb1.save('transactions2.xlsx')
Traceback (most recent call last):
File "C:/Users/Saman/PycharmProjects/Excel/excel_edit.py", line 23, in <module>
wb1.save('transactions2.xlsx')
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save
save_workbook(self, filename)
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook
writer.save()
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 276, in save
self.write_data()
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 76, in write_data
self._write_worksheets()
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 219, in _write_worksheets
self._write_drawing(ws._drawing)
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 142, in _write_drawing
self._archive.writestr(drawing.path[1:], tostring(drawing._write()))
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 283, in _write
anchor = _check_anchor(obj)
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 224, in _check_anchor
row, col = coordinate_to_tuple(anchor)
File "C:\Users\Saman\PycharmProjects\Excel\venv\lib\site-packages\openpyxl\utils\cell.py", line 201, in coordinate_to_tuple
return int(row), _COL_STRING_CACHE[col]
KeyError: 'f'
The case matters when naming columns - specifically, they are uppercase:
sheet.add_chart(chart, 'F2')
I changed f to F.
Just to add up
Check out their docs
You should use uppercase
sheet.add_chart(chart, 'F2')
import pandas as pd
import os
import time
from datetime import datetime
path = "C:\WinPython-32bit-2.7.9.5\python- 2.7.9\Lib\idlelib\MuditPracticals\intraQuarter\intraQuarter"
def Key_Stats(gather="Total Debt/Equity (mrq)"):
statspath = path+'/_KeyStats'
stock_list = [x[0] for x in os.walk(statspath)]
df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio','Price','SP500'])
sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv")
for each_dir in stock_list[1:25]:
each_file = os.listdir(each_dir)
ticker = each_dir.split("\\")[3]
if len(each_file) > 0:
for file in each_file:
date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')
unix_time = time.mktime(date_stamp.timetuple())
full_file_path = each_dir+'/'+file
source = open(full_file_path,'r').read()
try:
value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])
try:
sp500_date = datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d')
row = sp500_df[(sp500_df.index == sp500_date)]
sp500_value = float(row["Adjusted Close"])
except:
sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d')
row = sp500_df[(sp500_df.index == sp500_date)]
sp500_value = float(row["Adjusted Close"])
stock_price = float(source.split('</small><big><b>')[1].split('</b></big>')[0])
#print("stock_price:",stock_price,"ticker:", ticker)
df = df.append({'Date':date_stamp,
'Unix':unix_time,
'Ticker':ticker,
'DE Ratio':value,
'Price':stock_price,
'SP500':sp500_value}, ignore_index = True)
except Exception as e:
print "hello"
save = gather.replace(' ','').replace(')','').replace('(','').replace('/','')+('.csv')
print(save)
df.to_csv(save)
Key_Stats()
Compile Time Error In Spyder
File "<ipython-input-1-dfafbc7450e8>", line 1, in <module>
runfile('C:/WinPython-32bit-2.7.9.5/python- 2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py', wdir='C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals')
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py", line 56, in <module>
Key_Stats()
File "C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py", line 13, in Key_Stats
sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv")
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\core\frame.py", line 1036, in from_csv
infer_datetime_format=infer_datetime_format)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 474, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 250, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 566, in __init__
self._make_engine(self.engine)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 705, in _make_engine
``self._engine = CParserWrapper(self.f, **self.options)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 1072, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas\parser.pyx", line 350, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:3160)
File "pandas\parser.pyx", line 594, in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5905)
IOError: File YAHOO-INDEX_GSPC.csv does not exist
It is giving IO error though file exists at that location
IO ERROR occurs at compile time
and why it is so that in other IDLE pandas module is not found but in Spyder there is no pandas Error
the path to your .csv file is relative. if the file is not in your current working directory python will not find it.
"though file exists at that location"... that is the problem with relative paths: what is that location?
here is a previous answer that should resolve the issue:
Python ConfigParser cannot search .ini file correctly (Ubuntu 14, Python 3.4)