KeyError in python 3.7 - python

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.

Related

Iterating over folder names in Python

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

Not able to open a .xlsx file using the load_workbook by openpyxl?

I have a .xlsx sheet with a huge filename like this: child_id_survey_918_choose_touching_results_1541687696.xlsx when I try to use the load_workbook() to load this file it gives me a weird error.
My python file and the excel sheet both lie in the same folder.
CODE:
from openpyxl import load_workbook
workbook = load_workbook('child_id_survey_918_choose_touching_results_1541687696.xlsx')
Error:
Traceback (most recent call last):
File "test.py", line 2, in <module>
workbook = load_workbook('child_id_survey_918_choose_touching_results_1541687696.xlsx')
File "C:\Python38\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader.read()
File "C:\Python38\lib\site-packages\openpyxl\reader\excel.py", line 280, in read
self.read_worksheets()
File "C:\Python38\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets
ws_parser.bind_all()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_reader.py", line 434, in bind_all
self.bind_cells()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_reader.py", line 337, in bind_cells
for idx, row in self.parser.parse():
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_reader.py", line 153, in parse
row = self.parse_row(element)
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_reader.py", line 274, in parse_row
cells = [self.parse_cell(el) for el in row]
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_reader.py", line 274, in <listcomp>
cells = [self.parse_cell(el) for el in row]
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_reader.py", line 181, in parse_cell
style_id = int(style_id)
ValueError: invalid literal for int() with base 10: 'string'
I have also tried using this: workbook = load_workbook(filename = 'child_id_survey_918_choose_touching_results_1541687696.xlsx')
Why am I getting this error and how can I solve it?

KeyError when saving workbook

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')

Python -xlrd library - assert 0 <= colx < X12_MAX_COLS-AssertionError

Im using xlrd,xlwt,openpyxl for excel file manipulation
i have the method below, which goes over 30 files in folder and change specific column for each row in file (some file has 5,15,17,etc rows)
The method above can be run over and over and each file will be updated accordingly.
But,
Once i'm editing file (any of the files, and editing can be even small change in one of the cells (change from A to a for instance), the file cant be read by xlrd, and i'm getting the exception below.
Any idea why?
Exception:
Traceback (most recent call last):
File "/home/ohad/automationProj/automation-linux/0_master_code_prep.py", line 154, in
sanity_run_all_change()
File "/home/ohad/automationProj/automation-linux/0_master_code_prep.py", line 68, in sanity_run_all_change
ExcelWorkBook1 = open_workbook(config.STDFOLDER + '%s.xlsx'%suitename, on_demand=True)
File "/usr/local/lib/python2.7/dist-packages/xlrd/init.py", line 422, in open_workbook
ragged_rows=ragged_rows,
File "/usr/local/lib/python2.7/dist-packages/xlrd/xlsx.py", line 794, in open_workbook_2007_xml
x12sheet.process_stream(zflo, heading)
File "/usr/local/lib/python2.7/dist-packages/xlrd/xlsx.py", line 534, in own_process_stream
self.do_dimension(elem)
File "/usr/local/lib/python2.7/dist-packages/xlrd/xlsx.py", line 568, in do_dimension
rowx, colx = cell_name_to_rowx_colx(last_cell_ref)
File "/usr/local/lib/python2.7/dist-packages/xlrd/xlsx.py", line 91, in cell_name_to_rowx_colx
assert 0 <= colx < X12_MAX_COLS
AssertionError
Code:
def fullregression_run_all_change():
# FUll regression suites go over and change testes to Run
print "Go over Sanity suites and Change all Test to Run position"
ExcelWorkBook1 = open_workbook(config.UI_Suites_Location + 'STD_SUITES.xlsx', on_demand=True)
First_Sheet1 = ExcelWorkBook1.sheet_by_index(0)
Suite_List = []
for suitename in First_Sheet1._cell_values:
if suitename[1] == "Nightly Full Regression Run":
continue
else:
Suite_List.append(str(suitename[1]))
print "Full regression suites count is %s"%Suite_List.__len__()
for suitename in Suite_List:
ExcelWorkBook1 = open_workbook(config.STDFOLDER + '%s.xlsx'%suitename, on_demand=True)
First_Sheet1 = ExcelWorkBook1.sheet_by_index(0)
numberofrows=First_Sheet1.nrows
Startupdaterow=4
dest = config.STDFOLDER + suitename + ".xlsx"
wb = load_workbook(filename=dest)
ws = wb.get_active_sheet()
while Startupdaterow<=numberofrows:
ws.cell(row=Startupdaterow,column=8).value = 'RUN'
Startupdaterow +=1
wb.save(dest)
In my case the error was,
assert 1 <= nr <= self.utter_max_rows AssertionError
in .xls file in Python.
Visit this link to see my answer and do change in sheet.py for MAX-COLUMNS.
AssertionError with pandas when reading excel
I think you problem will be solved... :)

Key Error when trying to save in openpyxl - Corrupts excel file

I am trying to automate a report using openpyxl and am in the early stages. I have just set up the first part whereby i copy and paste a range of cells from one workbook to another. However when i go to save the workbook i am pasting into it returns a key error. It also corrupts my template file and renders it un-openable.
I have found a chap who has had the same problem however his was not resolved. Someone has instructed him to use version 1.8.5 however i could only find upto 1.8.2.
ERROR: Failure: KeyError (-8937945243006069197)
----------------------------------------------------------------------
Traceback (most recent call last):
File "//anaconda/lib/python2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
addr.filename, addr.module)
File "//anaconda/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "//anaconda/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Users/joegavin/Dropbox/Python/projects/project pymur/tests/sandbox_tests.py", line 53, in <module>
wb0.save('template.xlsx')
File "//anaconda/lib/python2.7/site-packages/openpyxl/workbook.py", line 265, in save
save_workbook(self, filename)
File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 187, in save_workbook
writer.save(filename)
File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 170, in save
self.write_data(archive)
File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 98, in write_data
self._write_worksheets(archive, shared_string_table, self.style_writer)
File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 128, in _write_worksheets
style_writer.get_style_by_hash()))
File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/worksheet.py", line 98, in write_worksheet
write_worksheet_cols(doc, worksheet, style_table)
File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/worksheet.py", line 209, in write_worksheet_cols
col_def['style'] = str(style_table[hash(columndimension.style_index)])
KeyError: -8937945243006069197
Here is all my code so far.
from openpyxl import load_workbook
from openpyxl import workbook
########################### CLASSES ####################################
class Institution(object):
def __init__(self, name, usage_hours,percentage_share,
target_percentage, number_of_projects):
self.name = name
self.usage_hours = usage_hours
self.percentage_share = percentage_share
self.target_percentage = target_percentage
self.number_of_projects = number_of_projects
class Project(object):
def __init__(self, project_name, project_code, percentage_share,
percentage_share_machine):
self.project_name = project_name
self.percentage_share = percentage_share
self.percentage_share_machine = percentage_share_machine
############################ Setup ########################################
# Loading the workbook to read the figures off
wb0 = load_workbook('figures.xlsx')
wb1 = load_workbook('template.xlsx')
# Navigating to the worksheet where we harvest the data
ws0 = wb0.worksheets[0]
# Navigating to the worksheet we will copy data to
ws1 = wb1.worksheets[0]
project_codes0 = ws0["B1":"B100"]
project_codes1 = ws1['B1':'B100']
#cells1 = cells0
#for i in range(0, len(cells0[0])):
#for cell in cells0:
#print cell[i].value
wb0.save('template.xlsx')
As per OP's comment
The problem was resolved by updating to the newest version 2.02

Categories