python xlsxwriter在使用write_row时改变所有的单元格宽度

如何使用write_row()定义整个“工作表”或每列的列宽?

例如,我有:

 workbook = xlsxwriter.Workbook(loc_path + 'monbatch_test1.xlsx') worksheet = workbook.add_worksheet() monbatch = [ [a,b,c,], [a,b,c,], [a,b,c,] ] row, col = 3, 0 for mon in monbatch: worksheet.write_row(row, col, mon, rows_format) row += 1 workbook.close() 

我想要更改所有列(a,b,c)的列宽。

有一个相关的set_column()方法接受width

set_column(first_col,last_col,width,cell_format,options)

设置一列或多列单元格的属性。

这里是你如何应用它:

 worksheet.set_column(0, 2, 100) 

解决的办法是

 def compute_rows(text, width): if len(text) < width: return 1 phrases = text.replace('\r', '').split('\n') rows = 0 for phrase in phrases: if len(phrase) < width: rows = rows + 1 else: words = phrase.split(' ') temp = '' for idx, word in enumerate(words): temp = temp + word + ' ' # check if column width exceeded if len(temp) > width: rows = rows + 1 temp = '' + word + ' ' # check if it is not the last word if idx == len(words) - 1 and len(temp) > 0: rows = rows + 1 return rows 

但是你可以在这里看到它: http : //assist-software.net/blog/how-export-excel-files-python-django-application