Python 3.5 openpyxl 2.4
Hi everyone, I got a simple but confusing problem here.
FYI the API doc relating to worksheet is
http://openpyxl.readthedocs.io/en/default/api/openpyxl.worksheet.worksheet.html
Here is some simple code for testing.
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
wb2 = load_workbook('example.xlsx')
print (wb2.get_sheet_names())
ws = wb2.get_sheet_by_name('Sheet1')
print (type(ws))
print (ws.calculate_dimension())
list = []
for i in ws.rows:
print ('\n')
for cell in i:
list.append(cell.value)
print(str(cell.value).encode('utf-8'))
print (type(ws))
ws.get_highest_row()
here's what turned out eventually
<class 'openpyxl.worksheet.worksheet.Worksheet'>
Traceback (most recent call last):
File "script.py", line 17, in <module>
ws.get_highest_row()
AttributeError: 'Worksheet' object has no attribute 'get_highest_row'
I run into the problem where it says that get_highest_row is not an attribute.
This seems correct since this function is under class worksheet.worksheet (from API doc), and ws is worksheet.worksheet.Worksheet (I've no idea what that is) may inherits some functions so it can still call dimension(), but can someone tell me how to fix this? I want to check through one specific row or column and do some sorting with varying length of cols and rows.
Any help is appreciated!
According to https://bitbucket.org/openpyxl/openpyxl/issues/278/get_highest_row-column-are-unreliable
In newest openpyxl, which has removed get_highest_row and get_highest_column method. They have been replaced by max_row and max_column property
I tried it with openpyxl 2.3.5 and got the following
/usr/local/lib/python3.5/site-packages/openpyxl/worksheet/worksheet.py:350:
UserWarning: Call to deprecated function or class get_highest_row (Use
the max_row property). def get_highest_row(self):
So as you are using 2.4 they probably removed it from there as it was deprecated already in 2.3.5.
EDIT: In the documentation for 2.4 this method is not mentioned any longer
Related
I am getting an error that seems weird. Worksheet object does have set_default_row() function, in the docs. Not sure what I am missing here.
I got this code project from someone who made this and has been running for a long time. We are using different python versions. He's on 3.10 and I am on 3.9 and I don't see that to be any reason.
Error:
Traceback (most recent call last):
File "C:\Users\ajoshi\my_folder\misc\quick tools\CI-TestRun-Report-Generator\FileProvider.py", line 31, in create
worksheet.set_default_row(20)
AttributeError: 'Worksheet' object has no attribute 'set_default_row'
Code:
s = data.style.applymap(FileProvider.color_negative_red)
s.to_excel(writer, sheet_name=plan["name"], header=True, index=False)
workbook = writer.book
worksheet = writer.sheets[plan["name"]]
worksheet.set_default_row(20)
worksheet.set_row(0, 40)
The issue is that you are calling a xlsxwriter method but that, most probably, the module isn't installed so Pandas is defaulting to creating a openpyxl worksheet object which has different APIs and doesn't have that method. Try set up your Pandas xlsx writer like this:
writer = pd.ExcelWriter('filename.xlsx', engine='xlsxwriter')
If that fails then you need to install xlsxwriter.
If you are already using engine='xlsxwriter' then the issue could be that you have a very old version installed that doesn't support the set_default_row() method. In which case you should upgrade xlsxwriter.
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]
i'm writing code for a too to perform GIS functions to an input of an excel sheet. sometimes the excel sheet will come in and have 2 separate rows across the top for its attributes fields, and when there is 2, I need to delete the top row. the value of cell A1 will be naming if I need to do this
I tried writing code to check this and delete it as below;
openpyxl
import arcpy, os, sys, csv, openpyxl
from arcpy import env
env.workspace = r"C:\Users\myname\Desktop\Yanko's tool"
arcpy.env.overwriteOutput = True
excel = r"C:\Users\myname\Desktop\Yanko's tool\Yanko's Duplicate tool\Construction_table_Example.xlsx"
layer = r"C:\Users\myname\Desktop\Yanko's tool\Yanko's Duplicate tool\Example_Polygons.shp"
output = r"C:\Users\myname\Desktop\Yanko's tool\\Yanko's Duplicate tool"
book = openpyxl.load_workbook(excel)
book.get_sheet_by_name("Construction Table format")
if ws.cell(row=1, column=1).value == "Naming":
ws.delete_rows(1, 1)
book.save
book.close
it should just delete the first row if the if function passes true, but I get the error;
Warning
(from warnings module):
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\openpyxl\reader\worksheet.py", line 310
warn(msg)
UserWarning: Data Validation extension is not supported and will be removed
Traceback (most recent call last):
File "C:\Users\ronan.corrigan\Desktop\Yanko's tool\Yanko's Duplicate tool\Yanko's Tool.py", line 31, in <module>
ws.delete_rows(1, 1)
AttributeError: 'Worksheet' object has no attribute 'delete_rows'
any help in figuring out what I've done wrong would be greatly appreciated
thanks
First of all, according to the docs, the get_sheet_by_name function is deprecated, and you should just be using the sheet name to get the function:
book["Construction Table format"]
Another thing to note, in your code I don't see you setting that ws value, which should be set to whatever sheet object is returned. If you're setting it somewhere else, so it may be possible that you are using a different sheet object which doesn't have that function
ws=book["Construction Table format"]
Other than that you'd have to share the stack trace to give a better understanding of what's breaking
With openpyxl, I am reading an excel file which has some filters applied already.
from openpyxl import load_workbook
wb = load_workbook('C:\Users\dsivaji\Downloads\testcases.xlsx')
ws = wb['TestCaseList']
print ws['B3'].value
My goal to loop through the content of the column 'B'. With this I will be able to read the content of the cell 'B3'. If filters applied and in that case, I don't want to start from the initial cell.
i.e. whichever visible in the excel (after applying the filters) , those alone I want to fetch.
After searching in web for sometime, found that ws.row_dimensions can help with the visible property, but still no luck.
>>> ws.row_dimensions[1]
<openpyxl.worksheet.dimensions.RowDimension object at 0x03EF5B48>
>>> ws.row_dimensions[2]
<openpyxl.worksheet.dimensions.RowDimension object at 0x03EF5B70>
>>> ws.row_dimensions[3].visible
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'RowDimension' object has no attribute 'visible'
How to achieve this ?
You are almost there. The name of the attribute is hidden. If you replace visible in your code with hidden, it should work.
openpyxl is a library for the OOXML file format (.xlsx) and not a replacement for an application like Microsoft Excel. As such support for filters is limited to reading and writing their definitions but not applying them.
I am already using an xlrd package. The code I am working on always returns an error message:
Traceback (most recent call last):
File "diffoct8.py", line 17, in <module>
row = rs.get(row_number)
AttributeError: 'Sheet' object has no attribute 'get'
What could be the problem?
Is there a newer version of XLRD?. If yes, how can I install it in Ubuntu?
Here you can get latest xlrd package. https://pypi.python.org/pypi/xlrd
From my understanding, you just want to get information from a row in a sheet. I assume there are 10 elements in a row.
Try this:
...
element_num = 10
row = []
for i in xrange(element_num):
row.append(rs.cell(row_number, i).value)
...
The method get() does not exist (It was purely used to show the approach you should take and where the problem was in your previous question). I've update my answer to that question to show you how you should use the row() method, as instructed in the documentation.