【发布时间】: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