简体   繁体   中英

How do you find the number of columns in an Excel xlsx file per row with Python openpyxl?

I was wondering if there is a way to determine the number of columns per row in Excel with Python. I know .max_row and .max_column show the maximum number of the respective fields. However, I need to pull the number of columns per row as they vary. The goal here is to create my own test program where all the Qs, As, and wrong As are in an excel spreadsheet and it is used as the database for the program. I'm not opposed to abandoning openpyxl if there is better tool.

for row in range(QAsheet.max_row):
    for column in range(QAsheet.max_column):
        columnletter=openpyxl.cell.get_column_letter(column+1)
        if(QAsheet["%s%s"%(columnletter,row+1)].value != None):
            print(QAsheet["%s%s"%(columnletter,row+1)].value)
        else:
            pass

This seems to work. Thanks for the help!

For openpyxl 2.4 and up, you can do this:

for col in range(QAsheet.max_column):
   print (row, len(QAsheet['row'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM