简体   繁体   English

如何使用Python openpyxl查找每行Excel xlsx文件中的列数?

[英]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. 我想知道是否有一种方法可以确定使用Python的Excel中每行的列数。 I know .max_row and .max_column show the maximum number of the respective fields. 我知道.max_row和.max_column显示了各个字段的最大数目。 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. 这里的目标是创建一个我自己的测试程序,其中所有Q,As和错误的As都存在于excel电子表格中,并将其用作程序的数据库。 I'm not opposed to abandoning openpyxl if there is better tool. 如果有更好的工具,我不反对放弃openpyxl。

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: 对于openpyxl 2.4及更高版本,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Openpyxl在xlsx中找不到数字 - Python Openpyxl can't find number in xlsx 您如何使用 python 在现有的 excel 文件中使用多列 append 新行? - How do you append a new row in an excisting excel file using python with multiple columns? Python OpenPyxl:如何遍历 1 Row 的列以查找值 - Python OpenPyxl: How to iterate through 1 Row's columns to find a value Python中使用openpyxl,如何返回当前行号? - Using openpyxl in Python, how do I return the current row number? 在 Excel 文件中,如何使用 openpyxl (Python) 查找单元格的背景颜色 - In a Excel file how to find the background color of a cell using openpyxl (Python) 如何使用 Openpyxl 更新现有的 Excel.xlsx 文件? - How to update existing Excel .xlsx file using Openpyxl? 如何使用 openpyxl 从字典创建 Excel 文件扩展名 .xlsx? - How to create an Excel file extension .xlsx from a dictionary using openpyxl? 在 Python (Pandas/Openpyxl) 中修改的 xlsx 文件与在 Excel 中修改的同一个 xlsx 文件具有不同的属性 - Xlsx file modified in Python (Pandas/Openpyxl) has not same properties as the same xlsx file modified in Excel 如何通过 Openpyxl 从 xlsx 文件导入 Python 类? - How do I import from xlsx file via Openpyxl into a Python Class? 如何在Openpyxl中找到最大行 - How do I find the max row in Openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM