我的excel文件包含了从A列到AL的第1行重复几次的一周中的几天。所以星期一,星期二,星期三,星期四,星期五,星期六,星期日,星期一,星期二.等。
它类似于日历,但想象一下水平阅读它。我只是在准备代码的早期阶段,因为我还没有添加每个日期的结果,所以星期二的第一天将与星期二8号不一样。
目前,我已经在开始阶段遇到了一个问题。这是我的代码:
import openpyxl
import os
os.chdir('C:\Python27\My files\work')
Week = {'Monday':'File1','Tuesday':'File2','Wednesday':'File3'}
wb = openpyxl.load_workbook('Calendar.xlsx')
sheet = wb.get_sheet_by_name('Report planning')
ws = wb.active
for colNum in ws.iter_rows('A1:AL1'):
dtrmnt = sheet.cell(column=colNum,row=1).value
if dtrmnt in Week:
sheet.cell(column=colNum, row=19).value = Week[dtrmnt]
wb.save('updatedcalendar.xlsx')我的错误:
Traceback (most recent call last):
File "C:/Python27/My files/work/calendar.py", line 16, in <module>
wb.save('updatedcalendar.xlsx')
File "C:\Python27\lib\site-packages\openpyxl\workbook\workbook.py", line 263, in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 239, in save_workbook
writer.save(filename, as_template=as_template)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 222, in save
self.write_data(archive, as_template=as_template)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 80, in write_data
self._write_worksheets(archive)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 163, in _write_worksheets
xml = sheet._write(self.workbook.shared_strings)
File "C:\Python27\lib\site-packages\openpyxl\worksheet\worksheet.py", line 776, in _write
return write_worksheet(self, shared_strings)
File "C:\Python27\lib\site-packages\openpyxl\writer\worksheet.py", line 198, in write_worksheet
dim = Element('dimension', {'ref': '%s' % worksheet.calculate_dimension()})
File "C:\Python27\lib\site-packages\openpyxl\worksheet\worksheet.py", line 420, in calculate_dimension
get_column_letter(max_col), max_row
File "C:\Python27\lib\site-packages\openpyxl\utils\__init__.py", line 101, in get_column_letter
raise ValueError("Invalid column index {0}".format(idx))
ValueError: Invalid column index (<Cell Report planning.A1>, <Cell Report planning.B1>, <Cell Report planning.C1>, <Cell Report planning.D1>, <Cell Report planning.E1>, <Cell Report planning.F1>, <Cell Report planning.G1>, <Cell Report planning.H1>, <Cell Report planning.I1>, <Cell Report planning.J1>, <Cell Report planning.K1>, <Cell Report planning.L1>, <Cell Report planning.M1>, <Cell Report planning.N1>, <Cell Report planning.O1>, <Cell Report planning.P1>, <Cell Report planning.Q1>, <Cell Report planning.R1>, <Cell Report planning.S1>, <Cell Report planning.T1>, <Cell Report planning.U1>, <Cell Report planning.V1>, <Cell Report planning.W1>, <Cell Report planning.X1>, <Cell Report planning.Y1>, <Cell Report planning.Z1>, <Cell Report planning.AA1>, <Cell Report planning.AB1>, <Cell Report planning.AC1>, <Cell Report planning.AD1>, <Cell Report planning.AE1>, <Cell Report planning.AF1>, <Cell Report planning.AG1>, <Cell Report planning.AH1>, <Cell Report planning.AI1>, <Cell Report planning.AJ1>, <Cell Report planning.AK1>, <Cell Report planning.AL1>)我是个新手,计算机编程不是我最好的技能。
发布于 2016-01-12 19:19:15
在for循环中,将整行作为列索引传递。ws.iter_rows()总是返回一行单元格。不清楚要写入哪一列,但表单应该类似于sheet.cell(column=1, row=1)
https://stackoverflow.com/questions/34748411
复制相似问题