Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88896

Writing a matrix in excel

$
0
0

I am trying to write an excel file from a python script.

I tried to make it with a 24x32 matrix (12 for all the months and one blank column and 32 for all possible days plus 1 more row for the name of the month on the top row)

I made a dictionary with all the month names and this year's month's length like this

months = {"JANUARY": 31, "FEBRUARY": 29, "MARCH": 31, "APRIL": 30, "MAY": 31, "JUNE": 30, "JULY": 31, "AUGUST": 31, "SEPTEMBER": 30, "OCTOBER": 31, "NOVEMBER": 30, "DECEMBER": 31}

And I wrote this function that writes to the excel file, but I get an error that I am trying to overwrite a cell and I can't understand what to do

    def write_to_excel():
        months = {"JANUARY": 31, "FEBRUARY": 29, "MARCH": 31, "APRIL": 30, "MAY": 31, "JUNE": 30, "JULY": 31, "AUGUST": 31, "SEPTEMBER": 30, "OCTOBER": 31, "NOVEMBER": 30, "DECEMBER": 31}

        # Create Workbook
        workbook = xlwt.Workbook()
        # Create new sheet
        sheet = workbook.add_sheet("Sheet 1")

        column = 0
        if column % 2 == 0:
            for month in range(len(months.keys())):
                sheet.write(0, column, [*months.keys()][month])
                for date in range(1, int([*months.values()][month]) + 1):
                    sheet.write(date, column, f"{date}/{month}/2020")
        else:
            column += 1

This is the error I get:

Traceback (most recent call last):
  File "C:/Users/user's_name/Desktop/app's_folder/app's_name.py", line 248, in <module>
    main_window()
  File "C:/Users/user's_name/Desktop/app's_folder/app's_name.py", line 240, in main_window
    response = approve_applications(raw_message)  # if it is get the email's information and project it on the window to be approved or denied
  File "C:/Users/user's_name/Desktop/app's_folder/app's_name.py", line 134, in approve_applications
    write_excel_file()
  File "C:/Users/user's_name/Desktop/app's_folder/app's_name.py", line 17, in write_excel_file
    sheet.write(column, 0, [*months.keys()][month])
  File "C:\Users\user's_name\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwt\Worksheet.py", line 1088, in write
    self.row(r).write(c, label, style)
  File "C:\Users\user's_name\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwt\Row.py", line 235, in write
    StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))
  File "C:\Users\user's_name\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwt\Row.py", line 154, in insert_cell
    raise Exception(msg)
Exception: Attempt to overwrite cell: sheetname='Sheet 1' rowx=0 colx=0

Under the month's name I want to write every day that it has like this day/month/2020

So the output should be:

And so on for each month

The month's name doesn't have to be a combined cell, I don't know if that helps in any way, and the sundays, don't have to be colored.

If someone could help I would appreciate it, thanks in advance.


Viewing all articles
Browse latest Browse all 88896

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>