[英]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.