繁体   English   中英

在Excel的日历中为一周中的每一天创建字典

[英]Creating a dictionary for each day of the week in a calendar on Excel

我的excel文件包含每周几天在第1行从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>)

我很新,并且计算机编程不是我的最佳技能。

for循环中,您将整行作为列索引传递。 ws.iter_rows()总是返回单元格的行。 尚不清楚您要写入哪一列,但表单应类似于sheet.cell(column=1, row=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM