【问题标题】:python program with output in an excel spreadsheet在 Excel 电子表格中输出的 python 程序
【发布时间】:2015-11-19 16:53:08
【问题描述】:

我正在尝试使用 Python 将从大型 Excel 工作簿中检索到的输出写入另一个电子表格。但是,我做不到,它给了我诸如 raise ValueError("column index (%r) not an int in range(256)" % arg)ValueError: column index (256) not an int in range(256) 之类的错误),异常:意外的数据类型。 我可以在一定程度上理解这些错误,但无法纠正我的代码。我在这里写了一个小脚本。如果有人能告诉我并纠正我哪里出错了,那就太好了。

import xlrd
import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "path/lookup_V1.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
print sheet1.name
sheet2 = workbook.sheet_by_index(1)
print sheet2.name
print workbook.nsheets
st1c=sheet1.ncols
st2r=sheet2.nrows
st1=st1c+1
st2=st2r+1
print("fill..")  

for i in xrange(0,st1c):
    s1=sheet1.col_values(i)
    i+1
    s1.sort()
    print s1

for col in xrange(st1c):
    for row in xrange(st2r):

    print("filling sheet...")    

    col=col+1
    row=row+1
    ws.write(row,col)
    print("here")
    wb.save('testfile.xls')

【问题讨论】:

  • 请包含整个回溯,以便我们查看它在哪一行。来回查看您的错误和您的代码并试图猜测计算机何时告诉您发生这种情况的确切位置并且您出于某种原因没有与我们分享,这并不有趣。
  • 如果您的代码缩进正确也会有所帮助
  • C:\Python2lib\sitepackages\spyderlib\widgets\externalshell\sitecustomize.py",第 540 行,在运行文件 execfile(filename, namespace)"/Testing.py",第 40 行,在 ws.write(row,col)File "C:\Python27\lib\site-packages\xlwt\Worksheet.py",第 1030 行,写入 self.row(r).write(c, label, style)File "C:\Python27\lib\site-packages\xlwt\Row.py",第 235 行,写入 self.__adjust_bound_col_idx(col 文件 "C:\Python27\lib\site-packages\xlwt\Row.py",行78, in_adjust_bound_col_idxraiseValueError("列索引 (%r) 不是 int in range(256)" % arg)ValueError: column index (256) not an int in range(256)
  • 将内容从答案转移到评论:col 和 row 是索引号吗?
  • 你不能做 st1=st1c+1 因为 st1c 还没有定义...

标签: python export-to-excel


【解决方案1】:

试试这个:

import xlrd
import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "test.xls"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
st1c=sheet1.ncols
st1r=sheet1.nrows

for col in xrange(st1c):
    for row in xrange(st1r):
        value = sheet1.col_values(col, row, row + 1)
        # According to documentation : http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Sheet.col_values-method
        # col_values returns a slice of the values of the cells in the given column.
        # That why we have to specify the index below, it is no elegant but it works
        ws.write(row, col, value[0]) 

wb.save('testfile.xls')

此解决方案适用于单个工作表...然后您可以将其转换为函数并迭代不同的工作表和工作簿...


更优雅的解决方案:

import xlrd
import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "test.xls"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
st1c=sheet1.ncols
st1r=sheet1.nrows

for col in xrange(st1c):
    for row in xrange(st1r):
        # More elegant
        value = sheet1.cell_value(row, col)
        ws.write(row, col, value) 

wb.save('testfile.xls')

【讨论】:

  • 我知道我在某个地方出错了,Python 编程新手,我只是想打印在将工作表 1 即 s1 排序到 Excel 工作表后检索到的值。输出打印到这里然后我相信它超出范围 aise ValueError("column index (%r) not an int in range(256)" % arg) ValueError: column index (256) not an int in range(256 )。
  • col 和 row 是我认为不是单元格或值内容的索引。所以用这个指针检查文档,我现在正在移动可以提供更多帮助,我会稍后尝试
  • 感谢您的帮助,是的,同意 row 和 col 是索引,我只是想将从下面提到的代码中检索到的输出发布到 xrange(0,st1c) 中 i 的 Excel 表中:s1 =sheet1.col_values(i) i+1 s1.sort() 打印 s1
  • 请注意,在打印语句可能出现问题之前您更改了索引 i+1
  • 我将索引更改为 I+1 以便遍历打开的工作簿的所有单元格(从中获取数据)。尝试删除迭代并没有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 2013-01-23
  • 2020-03-14
  • 2017-06-09
  • 1970-01-01
  • 2021-04-05
  • 2021-03-26
相关资源
最近更新 更多