I am using the following code to open an existing Excel file in python 3.6, Excel 2016:
Shnm = my_pyx.get_sheet_names()
sheet = my_pyx.get_sheet_by_name(Shnm[0])
from openpyxl import load_workbook
# Class to manage excel data with openpyxl.
class Copy_excel:
def __init__(self,src):
self.wb = load_workbook(src)
self.ws = self.wb.get_sheet_by_name(sheet)
self.dest="destination.xlsx"
# Write the value in the cell defined by row_dest+column_dest
def write_workbook(self,row_dest,column_dest,value):
c = self.ws.cell(row = row_dest, column = column_dest)
c.value = value
# Save excel file
def save_excel(self) :
self.wb.save(self.dest)
So when I do:
row_dest=2
column_dest=6
workbook = Copy_excel(my_file)
data=60
workbook.write_workbook(2,6,data )
workbook.save_excel()
where:
my_file is a str like filename.xlsx
and sheet is a str with the sheet name.
It bugs me with an error stating that the sheet name mentioned does not exist.
I also tried to replace:
self.ws = self.wb.get_sheet_by_name(sheet)
with
self.ws = self.wb[sheet]
but I still receive the same error.