Openpyxl not removing sheets from created workbook correctly - python

So I ran into an issue with remove_sheet() with openpxyl that I can't find an answer to. When I run the following code:
import openpyxl
wb = openpyxl.Workbook()
ws = wb.create_sheet("Sheet2")
wb.get_sheet_names()
['Sheet','Sheet2']
wb.remove_sheet('Sheet')
I get the following error:
ValueError: list.remove(x): x not in list
It doesn't work, even if I try wb.remove_sheet(0) or wb.remove_sheet(1), I get the same error. Is there something I am missing?

If you use get_sheet_by_name you will get the following:
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use
wb[sheetname]).
So the solution would be:
xlsx = Workbook()
xlsx.create_sheet('other name')
xlsx.remove(xlsx['Sheet'])
xlsx.save('some.xlsx')

remove.sheet() is given a sheet object, not the name of the sheet!
So for your code you could try
wb.remove(wb.get_sheet_by_name(sheet))
In the same vein, remove_sheet is also not given an index, because it operates on the actual sheet object.
Here's a good source of examples (though it isn't the same problem you're facing, it just happens to show how to properly call the remove_sheet method)!

Since the question was posted and answered, the Openpyxl library changed.
You should not use wb.remove(wb.get_sheet_by_name(sheet)) as indicated by #cosinepenguin since it is now depreciated ( you will get warnings when trying to use it ) but wb.remove(wb[sheet])

In python 3.7
import openpyxl
wb = openpyxl.Workbook()
ws = wb.create_sheet("Sheet2")
n=wb.sheetnames
#sheetname =>get_sheet_names()
wb.remove(wb["Sheet"])
'#or can use'
wb.remove(wb[n[1]])
1 is index sheet "sheet"
you can visit this link for more info

Related

Need some help opening excel files in python

Just having a strange issue. I am new in python and while running the below code. Geetting error. I have tried google but unable to run my code. Any advise please
import openpyxl
import os
os.chdir('/Users/omer/Documents/Python_Code/Udemy/Excel_Word_Pdf/')
workbook = openpyxl.load_workbook('example.xlsx')
sheet = workbook.get_sheet_by_name('Sheet1')
workbook.get_sheet_names()
cell = sheet['A1']
And the error i amgetting is
lesson42.py:13: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
sheet = workbook.get_sheet_by_name('Sheet1')
lesson42.py:15: DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
workbook.get_sheet_names()
I just tested the following. This should work.
import openpyxl
import os
workbook = openpyxl.load_workbook('test.xlsx')
sheet = workbook['Sheet1']
print(workbook.sheetnames)
cell = sheet['A1'].value
print(cell)
DeprecationWarning means that you're calling a function that's no longer supported. Go through their documentation to find the new function that's used to get_name, or try using pandas
Try viewing sheetnames first: workbook.sheetnames
This will give you a list of available sheets, then you can call them with workbook[#Some Sheet]

"Worksheet range names does not exist" KeyError in openpyxl

Let me preface this by saying I have tried looking for, and cannot seem to find a similar situation so please don't be too upset if this seems familiar to you. I am using Python 2.7 and openpyxl version 2.2.5 (I need to use 2.7, and used an older module for other reasons.)
I am new to Python and read/write code in general, so I'm testing this on the command line before I implement it:
I created a file, foo.xlsx in the Python27 file directory with some values that I manually entered via Excel.
I then used this simple code on the Python command line to test my code
from openpyxl import load_workbook
wb = load_workbook('foo.xlsx')
sheet_ranges = wb['range names']
It then resulted in the following error:
File "C:\Python27\lib\openpyxl\workbook.workbook.py", line 233 in getitem
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet sheet range names does not exist'
So I thought it had something to do with not importing the entire openpyxl module. I proceeded to do that and run the whole process but it resulted in the same error.
Can someone please let me know what I am doing wrong/how to solve this?
Additional information:
I had successfully written to an empty file before, and then read the values. This gave me the right values for everything EXCEPT what I had written in manually via Excel- the cells that had manual input returned None or Nonetype. The issue seems to be with cells with manual input.
I did hit save on the file before accessing it don't worry
This was in the same directory so I know that it wasn't a matter of location.
The following command does not make sense:
sheet_ranges = wb['range names']
Normally you open a workbook and then access one of the worksheets, the following gives you some examples on how this can be done:
import openpyxl
wb = openpyxl.Workbook()
wb = openpyxl.load_workbook(filename = 'input.xlsx')
# To display all of the available worksheet names
sheets = wb.sheetnames
print sheets
# To work with the first sheet (by name)
ws = wb[sheets[0]]
print ws['A1'].value
# To work with the active sheet
ws = wb.active
print ws['A1'].value
# To work with the active sheet (alternative method)
ws = wb.get_active_sheet()
print ws['A1'].value
If you want to display any named range in the workbook, you can do the following:
print wb.get_named_ranges()
I'm not exactly sure what it is you need to do, but to read Excel spreadsheets into python, I usually use xlrd (which to me was easier to get use to). See example:
import xlrd
workbook = xlrd.open_workbook(in_fname)
worksheet = workbook.sheet_by_index(0)
To write to Excel spreadsheets, I use xlsxwriter:
import xlsxwriter
workbook = xlsxwriter.Workbook(out_fname)
worksheet = workbook.add_worksheet('spreadsheet_name')
Hope this helps.

getting sheet names from openpyxl

I have a moderately large xlsx file (around 14 MB) and OpenOffice hangs trying to open it. I was trying to use openpyxl to read the content, following this tutorial. The code snippet is as follows:
from openpyxl import load_workbook
wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'big_data')
The problem is, I don't know the sheet name, and Sheet1/Sheet2.. etc. didn't work (returned NoneType object). I could not find a documentation telling me How to get the sheet names for an xlsx files using openpyxl. Can anyone help me?
Use the sheetnames property:
sheetnames
Returns the list of the names of worksheets in this workbook.
Names are returned in the worksheets order.
Type: list of strings
print (wb.sheetnames)
You can also get worksheet objects from wb.worksheets:
ws = wb.worksheets[0]
As a complement to the other answers, for a particular worksheet, you can also use cf documentation in the constructor parameters:
ws.title
python 3.x
for get sheet name you must use attribute
g_sheet=wb.sheetnames
return by list
for i in g_sheet:
print(i)
**shoose any name **
ws=wb[g_sheet[0]]
or ws=wb[any name]
suppose name sheet is paster
ws=wb["paster"]
As mentioned the earlier answer
you can get the list of sheet names
by using the ws.sheetnames
But if you know the sheet names you can get that worksheet object by
ws.get_sheet_by_name("YOUR_SHEET_NAME")
Another way of doing this is as mentioned in earlier answer
ws['YOUR_SHEET_NAME']
for worksheet in workbook:
print(worksheet.name)

How to access the real value of a cell using the openpyxl module for python

I am having real trouble with this, since the cell.value function returns the formula used for the cell, and I need to extract the result Excel provides after operating.
Thank you.
Ok, I think I ahve found a way around it; apparently to access cell.internal value you have to use the iter_rows() in your worksheet previously, which is a list of "RawCell".
for row in ws.iter_rows():
for cell in row:
print cell.internal_value
Like Charlie Clark already suggest you can set data_only on True when you load your workbook:
from openpyxl import load_workbook
wb = load_workbook("file.xlsx", data_only=True)
sh = wb["Sheet_name"]
print(sh["x10"].value)
From the code it looks like you're using the optimised reader: read_only=True. You can switch between extracting the formula and its result by using the data_only=True flag when opening the workbook.
internal_value was a private attribute that used to refer only to the (untyped) value that Excel uses, ie. numbers with an epoch in 1900 for dates as opposed to the Python form. It has been removed from the library since this question was first asked.
You can try following code.Just provide the excel file path and the location of the cell which value you need in terms of row number and column number below in below code.
from openpyxl import Workbook
wb = Workbook()
Dest_filename = 'excel_file_path'
ws=wb.active
print(ws.cell(row=row_number, column=column_number).value)
Try to use cell.internal_value instead.
Please use this below in Python, and you can get the real values with openpyxl module:
for row in ws.iter_rows(values_only=True):
for cell in row:
print(cell)

Getting values from a column in excel using openpyxl

I had intended to do this using the code in the answer here, in the last block of code. However i get an error in the line for cell in ws.iter_rows(range_string=range_expr): saying that "Worksheet object has no attribute iter_rows". Any idea what I'm doing wrong here?
all i needed was to change the workbook declaration to the following: wb = load_workbook('path/doc.xls', use_iterators=True), adding in the use_iterators paramater. Simple issue, simple solution :)

Categories