栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python xlwt-访问现有单元格内容,自动调整列宽

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python xlwt-访问现有单元格内容,自动调整列宽

我刚刚实现了一个包装器类,该类可以在您输入物品时跟踪它们的宽度。看来效果很好。

import arial10class FitSheetWrapper(object):    """Try to fit columns to max size of any entry.    To use, wrap this around a worksheet returned from the     workbook's add_sheet method, like follows:        sheet = FitSheetWrapper(book.add_sheet(sheet_name))    The worksheet interface remains the same: this is a drop-in wrapper    for auto-sizing columns.    """    def __init__(self, sheet):        self.sheet = sheet        self.widths = dict()    def write(self, r, c, label='', *args, **kwargs):        self.sheet.write(r, c, label, *args, **kwargs)        width = arial10.fitwidth(label)        if width > self.widths.get(c, 0): self.widths[c] = width self.sheet.col(c).width = width    def __getattr__(self, attr):        return getattr(self.sheet, attr)

所有的魔力都在杨John的

arial10
模块中。对于默认的Excel字体Arial
10,它具有良好的宽度。如果要使用其他字体编写工作表,则需要更改fitwidth函数,最好考虑
style
传递给的参数
FitSheetWrapper.write



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/650659.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号