在 Excel 的日历中为一周中的每一天创建字典
Creating a dictionary for each day of the week in a calendar on Excel
我的 excel 文件包含从 A 列到 AL 的第 1 行重复几次的星期几。所以周一、周二、周三、周四、周五、周六、周日、周一、周二....等等
它类似于日历,但想象一下它是水平阅读的。我只是在准备代码的早期阶段,因为我还没有添加每个日期播放的内容,因此星期二 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>)
我很新,计算机编程不是我最好的技能。
在您的 for
循环中,您将整行作为列索引传递。 ws.iter_rows()
总是 returns 行单元格。不清楚您想写到哪一列,但表格应该类似于 sheet.cell(column=1, row=1)
我的 excel 文件包含从 A 列到 AL 的第 1 行重复几次的星期几。所以周一、周二、周三、周四、周五、周六、周日、周一、周二....等等
它类似于日历,但想象一下它是水平阅读的。我只是在准备代码的早期阶段,因为我还没有添加每个日期播放的内容,因此星期二 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>)
我很新,计算机编程不是我最好的技能。
在您的 for
循环中,您将整行作为列索引传递。 ws.iter_rows()
总是 returns 行单元格。不清楚您想写到哪一列,但表格应该类似于 sheet.cell(column=1, row=1)