Python Openpyxl sheet.dimensions

Python Openpyxl sheet.dimensions



I have a spreadsheet with data and want to know the first and last cell of the range that contains data. I use Python with Openpyxl. The sheet.dimensions property returns the top-left and bottom-right cell of the area of non-empty cells.
How can I put the outcome of sheet.dimensions in 2 variables. Example: result of sheet.dimensions is A2:C20.
I need one variable containing the first cell of the range (in this case A2) and the other variable containing the last cell of the range (in this case C20).
I expected something like sheet.dimensions.first and .last but so far I can not find the answer.



the spreadsheet



The program to its basics is as follows:



import openpyxl



book = openpyxl.load_workbook('test 1.xlsx', data_only=True)



sheet = book.active



print (sheet.dimensions)



book.save("result test 1.xlsx")



When the program is run it gives the result A2:C20.





Can you show us what you've tried so far? It would be nice to see a MCVE.
– colidyre
Aug 29 at 11:32




1 Answer
1



I found that openpyxl has the following attributes:



These are exactly the numbers that I need and with these I know how many iterations are needed to process the whole worksheet.
Example:



import openpyxl



book = openpyxl.load_workbook('test 1.xlsx', data_only=True)



sheet = book.active



mi_row = sheet.min_row



ma_row = sheet.max_row



mi_col = sheet.min_column



ma_col = sheet.max_column



print(mi_row, mi_col, ma_row, ma_col)



for row in sheet.iter_rows(min_row=mi_row, min_col=mi_col, max_row=ma_row, max_col=ma_col):


for cell in row:

pop = (cell.value)

print(pop)



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ