1
我目前正在使用Python創建Excel文檔。我創建了Excel文檔,但是我不確定代碼中的問題是它沒有正確調整列的大小。有沒有人有任何想法?使用Python調整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
Excel列的大小不是按字母數量大小。那是你的問題。如果您使用的是等寬字體,則可以很好地估計比例因子,但否則您必須猜測寬度值。 –
你應該用你用來處理Excel文件的Python包標記你的問題。 (或者在你的代碼片段中包含相關的'import'語句,或者在你的問題的文本中提及它。) –
我沒有想到@JohnY。謝謝 –