Python Forum

Full Version: using openpyxl
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am very interested in Python applied to spreadsheets, because I do simple, boring tasks with spreadsheets related to school and students. I got this code from Automate the Boring Stuff.

This must be a version problem I think. I looked at https://docs.python.org/3.5/
but I can't find the relevant documentation.

>>> wb = openpyxl.load_workbook('examples.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet.get_highest_row()
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    sheet.get_highest_row()
AttributeError: 'Worksheet' object has no attribute 'get_highest_row'
Can someone please just point me at the right docs, I will read them!
please post enough code so that it can be run by moderators.
You can find the documentation here: https://openpyxl.readthedocs.io/en/default/

Thank you
This is from the book, to be tried in the interactive shell. There is an excel file examples.xlsx in the cwd

>>> import openpyxl
>>> wb = openpyxl.load_workbook('example.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet.get_highest_row()
7
>>> sheet.get_highest_column()
3
"Note that the get_highest_column() method returns an integer rather
than the letter that appears in Excel."

However, I just get an error.
Did you try to run interactively, or from a script?
I did this in the interactive shell.

A bit further in the book I need sheet.get_highest_row() again, but I just get

Quote:Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
sheet.get_highest_row()
AttributeError: 'Worksheet' object has no attribute 'get_highest_row'

Quote:#! python3
# readCensusExcel.py - Tabulates population and number of census tracts for
# each county.
u import openpyxl, pprint
print('Opening workbook...')
v wb = openpyxl.load_workbook('censuspopdata.xlsx')
w sheet = wb.get_sheet_by_name('Population by Census Tract')
countyData = {}
# TODO: Fill in countyData with each county's population and tracts.
print('Reading rows...')
x for row in range(2, sheet.get_highest_row() + 1):
# Each row in the spreadsheet has data for one census tract.
state = sheet['B' + str(row)].value
county = sheet['C' + str(row)].value
pop
= sheet['D' + str(row)].value
# TODO: Open a new text file and write the contents of countyData to it.
The errors above to not match the code you supplied
In addition, the error seems incomplete
without that we can be of little help
Oh well, I'll get to the bottom of it eventually!

That is the entire error as it appears in the interactive shell:

Quote:>>> sheet.get_highest_row()
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
sheet.get_highest_row()
AttributeError: 'Worksheet' object has no attribute 'get_highest_row'
>>>
I think I'm looking at the Automate the Boring stuff page here: https://automatetheboringstuff.com/chapter12/
correct me if I'm wrong.
I'm going to do their tutorial and see if I can figure out what's going on.
I'll be back after that.
Ok,

Here's what I've done. Here's the code:
import openpyxl

wb = openpyxl.load_workbook('example.xlsx')
sheetnames = wb.get_sheet_names()
print('sheetnames: {}'.format(sheetnames))

sheet = wb.get_sheet_by_name('Sheet3')
print('sheet: {}'.format(sheet))
print('sheet data type: {}'.format(type(sheet)))

title = sheet.title
print('title: {}'.format(sheet.title))

anotherSheet = wb.active
print('anotherSheet: {}'.format(anotherSheet))
And the results:
Output:
sheetnames: ['Sheet1', 'Sheet2', 'Sheet3'] sheet: <Worksheet "Sheet3"> sheet data type: <class 'openpyxl.worksheet.worksheet.Worksheet'> title: Sheet3 anotherSheet: <Worksheet "Sheet1">
I use python 3.6.2
Thanks for going to all that trouble! The part you posted above works for me too, I use Ubuntu 16 and python 3.5

Did you try: sheet.get_highest_row() ??

I made my own spreadsheet, but that should not cause this error, it loads and reads ok. I can imagine situations where it is necessary to get the highest row and column before any manipulation with a 'for cell in range(1, highest_row)' loop, so there must be a routine to do that, I presume.
Pages: 1 2