Introduction to Excel

Excel - Chapter 1

We appreciate your patience while we actively develop and enhance our content for a better experience.

Sample Code
import openpyxl

def main():
    print("This program demonstrates how to use Openpyxl to create an Excel file, write data to it, and save the file.")

    # Create a new Excel workbook and a new sheet
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = "Student Grades"

    # Write data to the sheet (headers and example data)
    headers = ['Name', 'Math', 'Science', 'English']
    students = [
        ['Alice', 85, 90, 82],
        ['Bob', 78, 82, 91],
        ['Charlie', 92, 88, 78],
        ['David', 76, 75, 88],
        ['Eva', 89, 93, 80]
    ]

    # Write headers
    for col, header in enumerate(headers, start=1):
        sheet.cell(row=1, column=col, value=header)

    # Write student data
    for row, student in enumerate(students, start=2):
        for col, value in enumerate(student, start=1):
            sheet.cell(row=row, column=col, value=value)

    # Save the workbook to a file
    workbook.save("Student_Grades.xlsx")

    print("Excel file created: Student_Grades.xlsx")

if __name__ == "__main__":
    main()