python - 使用 Python 调整 Excel 文档中的列大小

标签 python excel python-2.7 openpyxl

我目前正在使用 Python 创建 Excel 文档。我创建了 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

最佳答案

我找到了我正在从事的工作所需的东西。 我需要在检查或分配列宽的区域添加“.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

关于python - 使用 Python 调整 Excel 文档中的列大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078902/

相关文章:

python - 属性错误: module 'multiprocessing' has no attribute 'Event'

python - Pandas 在重复 DataFrame.reset_index() 上崩溃

PHP:读取大量 Excel 文件导致 fatal error

vba - Excel VBA脚本清除列中的所有复选框

python - 使用 py.test 运行测试套件(任意测试集合)

python - 将字符串解析为字典

java - 不使用InputStream更新Java Apache POI中的现有Excel文件

python - 从 dict 以列表作为值创建 DataFrame

python - 如何从 Windows 中彻底删除 Anaconda3?

python - 在瓶中使用 POST 方法时获取 405 方法不允许