【问题标题】:How to not exceed the maximum number of fonts when generating XLS spreadsheets生成 XLS 电子表格时如何不超过最大字体数
【发布时间】:2017-07-07 05:15:26
【问题描述】:

我正在获取几个以逗号分隔的 CSV 文件,并使用它们生成一个 XLS 电子表格,其中文件的名称成为电子表格中的单独选项卡。我的代码产生了我想要的结果,除了打开电子表格时我收到以下警告:“由于超出了最大字体数,此文件中的某些文本格式可能已更改。它可能有助于关闭其他文档并重试。”我很确定问题出在代码试图更改超出 65536 行限制的单元格格式,但我不确定如何限制行更改。我需要在四列中不超过几百行。

import csv, glob, xlwt, sys, os
csvFiles = os.path.join(LogFileFolder, "*")
wb = xlwt.Workbook()
colNames = ['iNFADS_FAC','CAT','Crosswalk_FAC','FAC']
for filename in glob.glob(csvFiles):
    (f_path, f_name) = os.path.split(filename)
    (f_short_name, f_extension) = os.path.splitext(f_name)
    ws = wb.add_sheet(f_short_name)
    with open(filename, 'rb') as csvf: 
        csvReader = csv.reader(csvf)
        for rowx, row in enumerate(csvReader): 
            for colx, value in enumerate(row):
                if value in colNames:
                    ws.write(rowx, colx, value, xlwt.easyxf(
                        "border: top medium, right medium, bottom double, left medium; 
                        font: bold on; pattern: pattern solid, fore_color pale_blue; 
                        align: vert centre, horiz centre"))
                elif value not in colNames:
                    ws.write(rowx, colx, float(value), 
                        xlwt.easyxf("align: vert centre, horiz centre"))
##This second "xlwt.easyxf(align...)" part is the offending section of the code, if
##I remove just that part then the problem goes away. Is there a way to keep
##it within the 65536 limit here?
                else:
                    pass
wb.set_active_sheet = 1      
outXLS = os.path.join(LogFileFolder, "FAC-CAT Code Changes.xls")
wb.save(outXLS) 

【问题讨论】:

    标签: csv python-2.7 excel


    【解决方案1】:

    我要感谢 Google Group 'python-excel' 的 John Machin 回答了我的问题。显然,解决方案是将 easyxf 部分移动到脚本中较早的变量中,然后在需要时调用它。所以脚本应该是:

    csvFiles = os.path.join(LogFileFolder, "*")
    wb = xlwt.Workbook()
    headerStyle = xlwt.easyxf("border: top medium, right medium, bottom double," \ 
        "left medium; font: bold on; pattern: pattern solid, fore_color pale_blue;" \ 
        "align: vert centre, horiz centre")
    valueStyle = xlwt.easyxf("align: vert centre, horiz centre")
    colNames = ['iNFADS_FAC','CAT','Crosswalk_FAC','FAC']
    for filename in glob.glob(csvFiles):
        (f_path, f_name) = os.path.split(filename)
        (f_short_name, f_extension) = os.path.splitext(f_name)
        ws = wb.add_sheet(f_short_name)
        with open(filename, 'rb') as csvf:
            csvReader = csv.reader(csvf)
            for rowx, row in enumerate(csvReader): 
                for colx, value in enumerate(row):
                    if value in colNames:
                        ws.col(colx).width = 256 * 15
                        ws.write(rowx, colx, value, headerStyle)
                    elif value not in colNames:
                        ws.write(rowx, colx, float(value), valueStyle)
                    else:
                        pass
    wb.set_active_sheet = 1      
    outXLS = os.path.join(LogFileFolder, "FAC-CAT Code Changes.xls")
    wb.save(outXLS) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多