Is it possible to get an Excel document's row count without loading the entire document into memory?

excel spreadsheet
excel document recovery
where are temporary excel files stored
where are temporary excel files stored in windows 10
recover unsaved excel file 2007
unsaved excel file not in recovery
how to insert excel file in word as attachment
how to recover deleted excel file

I'm working on an application that processes huge Excel 2007 files, and I'm using OpenPyXL to do it. OpenPyXL has two different methods of reading an Excel file - one "normal" method where the entire document is loaded into memory at once, and one method where iterators are used to read row-by-row.

The problem is that when I'm using the iterator method, I don't get any document meta-data like column widths and row/column count, and i really need this data. I assume this data is stored in the Excel document close to the top, so it shouldn't be necessary to load the whole 10MB file into memory to get access to it.

So, is there a way to get ahold of the row/column count and column widths without loading the entire document into memory first?

Adding on to what Hubro said, apparently get_highest_row() has been deprecated. Using the max_row and max_column properties returns the row and column count. For example:

    wb = load_workbook(path, use_iterators=True)
    sheet = wb.worksheets[0]

    row_count = sheet.max_row
    column_count = sheet.max_column

How to Make a Spreadsheet in Excel, Word, and Google Sheets , If you want to be able to use Excel functions and other Excel formatting features later to update the table, you're better off pasting the Excel data  In some instances, it is possible to get the Excel file back using the Excel software. To use this method, you will need to follow the steps below: Step 1: Open Excel. Step 2: Select File > Info > Manage Document > Recover Unsaved Workbooks. Step 3: Select the file you are interested in and then hit the Open button. Step 4: In the bar that appears at the top of the page, select Save As to save the Excel file.

The solution suggested in this answer has been deprecated, and might no longer work.

Taking a look at the source code of OpenPyXL (IterableWorksheet) I've figured out how to get the column and row count from an iterator worksheet:

wb = load_workbook(path, use_iterators=True)
sheet = wb.worksheets[0]

row_count = sheet.get_highest_row() - 1
column_count = letter_to_index(sheet.get_highest_column()) + 1

IterableWorksheet.get_highest_column returns a string with the column letter that you can see in Excel, e.g. "A", "B", "C" etc. Therefore I've also written a function to translate the column letter to a zero based index:

def letter_to_index(letter):
    """Converts a column letter, e.g. "A", "B", "AA", "BC" etc. to a zero based
    column index.

    A becomes 0, B becomes 1, Z becomes 25, AA becomes 26 etc.

    Args:
        letter (str): The column index letter.
    Returns:
        The column index as an integer.
    """
    letter = letter.upper()
    result = 0

    for index, char in enumerate(reversed(letter)):
        # Get the ASCII number of the letter and subtract 64 so that A
        # corresponds to 1.
        num = ord(char) - 64

        # Multiply the number with 26 to the power of `index` to get the correct
        # value of the letter based on it's index in the string.
        final_num = (26 ** index) * num

        result += final_num

    # Subtract 1 from the result to make it zero-based before returning.
    return result - 1

I still haven't figured out how to get the column sizes though, so I've decided to use a fixed-width font and automatically scaled columns in my application.

How to Insert an Excel Worksheet into a Word Doc, The Find link takes you to that place in your worksheet, and the Help link takes you to information on the issue and possible solutions. Notes: In your new version of  Step 7: Decide where you want to save your new Excel document. Either pick one of the Recent Folders on show, or click the blue Choose a Different Folder button. In the resulting pop-up file

This might be extremely convoluted and I might be missing the obvious, but without OpenPyXL filling in the column_dimensions in Iterable Worksheets (see my comment above), the only way I can see of finding the column size without loading everything is to parse the xml directly:

from xml.etree.ElementTree import iterparse
from openpyxl import load_workbook
wb=load_workbook("/path/to/workbook.xlsx", use_iterators=True)
ws=wb.worksheets[0]
xml = ws._xml_source
xml.seek(0)

for _,x in iterparse(xml):

    name= x.tag.split("}")[-1]
    if name=="col":
        print "Column %(max)s: Width: %(width)s"%x.attrib # width = x.attrib["width"]

    if name=="cols":
        print "break before reading the rest of the file"
        break

Save an Excel workbook for compatibility with earlier versions of , The above steps would add your Excel file to the drive and now you will be able to  If you update the Excel file, those updates get automatically reflected in the target document. If you embed an Excel worksheet in a document, that connection is broken. Updating the original Excel sheet does not automatically update the data in the target document. There are advantages to both methods, of course.

Python 3

import openpyxl as xl

wb = xl.load_workbook("Sample.xlsx", enumerate)

#the 2 lines under do the same. 
sheet = wb.get_sheet_by_name('sheet') 
sheet = wb.worksheets[0]

row_count = sheet.max_row
column_count = sheet.max_column

#this works fore me.

How to Convert Excel to Google Sheets (Step-by-Step , Afterward, you'll be able to access them as normal. excel hidden sheet. 4. Make Sure to Grab a List of Passwords. If you're taking ownership of  If possible, retrieve a backup copy - you do make backups regularly, don't you? Something else to try: Right-click the overwritten file in File Explorer. Select Properties from the context menu. Activate the Previous Versions tab. If a previous version is available, select it and open it to check if it is the one you want.

https://pythonhosted.org/pyexcel/iapi/pyexcel.sheets.Sheet.html see : row_range() Utility function to get row range

if you use pyexcel, can call row_range get max rows.

python 3.4 test pass.

5 Excel Document Settings You Must Never Forget to Check, If you have Excel 2007, click the round Office button in the very top left of Excel Saving it to OneDrive means you'll also be able to open it when you get home  With that, let's get started. Creating a New Word Document Online. To start using Office for free, all you've got to do is open your browser, go to Office.com, and select the app you want to use. There's online copies of Word, Excel, PowerPoint, and OneNote you can choose from, as well as contacts and calendar apps and the OneDrive online storage.

How to save your Microsoft Excel spreadsheet, There are essentially three ways that I am aware of to get data from the data to a new Excel, csv or text file, and then copy and paste the data into if it is possible to pull a list of data into one cell and make it a drop down. If you want to quickly convert a whole Excel worksheet to Word document without opening Excel, you can insert the excel worksheet as an object in Word. Notes: (1) By inserting as object, you only can insert an entire worksheet at once time. (2) It will insert the last active worksheet when Excel workbook is closed as the object. 1.

Pull External Data into Excel, You will be able to merge your changes at a later point. How to force recent changes to override previous changes automatically. To have the  See below how to retrieve an overwritten Excel file: Click on the FILE tab and choose Info on the left-hand pane. Next to the Manage Versions button you'll see all autosaved versions of your document. Excel automatically saves versions of the workbook at specified intervals, but only if you've made changes to your spreadsheet between these intervals.

Excel shared workbook: How to share Excel file for multiple users, The tutorial describes 4 possible ways to convert Excel files to PDF – by So, if you have any modern version installed, including Excel 2016,  How to Convert Excel to PDF. Converting an Excel file to a PDF will allow anyone to open it, even if they don't have Microsoft Office installed. It can also make for easier printing and distribution of your Excel spreadsheet.

Comments
  • I have a feeling that if you have huge Excel files, you're likely using Excel for a task it is unsuited to.
  • @Markus: That's not really relevant. My boss is using Excel, I'm just writing this script for him.
  • In any case, I had a browse through openpyxl, and it doesn't seem to load the column dimensions for IterableWorksheet. If you load the whole thing at once you can get the dimensions like worksheet.column_dimensions["A"].width, however the column_dimensions dict is completely unpopulated for the iterable worksheet. :-/ It looks like the newer excel documents are just XML so you could in theory use that to look for your column elements and extract the info directly, but it's a hassle.
  • Since when is 10MB huge?
  • @MadPhysicist 10MB is actually moderately big for xlsx files. Remember, they are compressed XML. So a 10MB xlsx could potentially unpack to >100mb when loaded (especially if it contains non-primitive objects). I have worked with XLSX in the 90MB range though...
  • max_row and max_column didn't work for sheet = wb.active. I am using openpyxl==2.4.8
  • @Hussain: What value did you get and what did you expect? And what about sheet = wb.worksheets[0]?
  • @Hussain sheet = wb.active worked fine for me using that version
  • but in this case, you're counting None valued cells as well, I tried to loop through the columns instead, I know it is not the best way. but it is useful for me.
  • In hindsight, subtracting 1 from sheet.get_highest_row() to get the row count was probably incorrect. Since the row numbers were 1-based, and not 0-based, the highest row index would also be the row count. There might have been a valid reason for subtracting 1 though, I can't recall.