簡體   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