使用Python调整Excel文档中的列

我目前正在用Python创build一个Excel文档。 我创build了Excel文档,但是我不确定代码中有什么问题,而不是正确调整列的大小。 有没有人有任何想法?

def writerow(self, vals): ws = self.workbook.active this_row = self.numrows this_col = 1 for v in vals: cell = ws.cell(row = this_row, column = this_col) cell.value = v if ws.column_dimensions[get_column_letter(this_col)] < len(str(v)): ws.column_dimensions[get_column_letter(this_col)] = len(str(v)) this_col += 1 self.numrows += 1 self.worksheet = ws 

我find了我所需要的东西。 我需要将“.width”添加到检查或分配列宽的区域。

  def writerow(self, vals): ws = self.workbook.active this_row = self.numrows this_col = 1 for v in vals: cell = ws.cell(row = this_row, column = this_col) cell.value = v print "Column Width:" print ws.column_dimensions[get_column_letter(this_col)].width if ws.column_dimensions[get_column_letter(this_col)].width < len(str(v)): ws.column_dimensions[get_column_letter(this_col)].width = len(str(v)) this_col += 1 self.numrows += 1 self.worksheet = ws