├── Data ├── pie.xlsx ├── Mark.xlsx ├── Marks.xlsx ├── Sample_file.xlsx ├── Student_details.xlsx ├── student_result.xlsx ├── Student_Record_1.xlsx ├── Student_Record_2.xlsx └── Student_details_with_marks.xlsx ├── output ├── Failed.xlsx ├── 1st class.xlsx ├── 2nd class.xlsx └── 3rd class.xlsx ├── README.md └── Excel_Operations.ipynb /Data/pie.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/pie.xlsx -------------------------------------------------------------------------------- /Data/Mark.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Mark.xlsx -------------------------------------------------------------------------------- /Data/Marks.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Marks.xlsx -------------------------------------------------------------------------------- /output/Failed.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/output/Failed.xlsx -------------------------------------------------------------------------------- /Data/Sample_file.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Sample_file.xlsx -------------------------------------------------------------------------------- /output/1st class.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/output/1st class.xlsx -------------------------------------------------------------------------------- /output/2nd class.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/output/2nd class.xlsx -------------------------------------------------------------------------------- /output/3rd class.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/output/3rd class.xlsx -------------------------------------------------------------------------------- /Data/Student_details.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Student_details.xlsx -------------------------------------------------------------------------------- /Data/student_result.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/student_result.xlsx -------------------------------------------------------------------------------- /Data/Student_Record_1.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Student_Record_1.xlsx -------------------------------------------------------------------------------- /Data/Student_Record_2.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Student_Record_2.xlsx -------------------------------------------------------------------------------- /Data/Student_details_with_marks.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajansahu713/Excel-Automation-With-Python/HEAD/Data/Student_details_with_marks.xlsx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Excel Automation With Python 2 | 3 | ## Table of Content: 4 | 1. Write data using openpyxl 5 | 2. Write data using pandas 6 | 3. Read data using openpyxl 7 | 4. Read data using pandas 8 | 5. Creating Multiple Sheets in master excel file 9 | 6. Combining multiple excel speadsheet into a master excel 10 | 7. Plotting bar graph 11 | 8. Plotting pie graph 12 | 9. Sample Project 1 13 | 10. Sample Project 2 14 | 15 | 16 | ## 9. Simple Project 1 17 | Task 1: we are going to add two more column in excel spreadsheet **Result** and **Grade**. 18 | 19 | > In **Result** column we will put **pass** and **Failed** based on Percentage He/She got.
20 | 1. **Pass**: If percentage is greater than and equal to 40. 21 | 2. **Failed**: If percentage is less than 40 22 | 23 | > In **Grade** column we will put **1st class**, **2nd class**, **3rd class** and **Failed** based on Percentage He/She got. 24 | 1. 1st class: If percentage greater than equal to 60. 25 | 2. 2nd class: If percentage greater than equal to 50 but less than 60. 26 | 3. 3rd class: If percentage greater than equal to 40 but less than 50. 27 | 4. Failed: If percentage less than 40. 28 | 29 | Task 2: After doing the first task we will create multiple sheet in the same excel file base on Grade He/She got 30 | 31 | ## 10. Simple Project 2 32 | In this project we are separating the records in different excel spreadsheet for different **Grade**. 33 | All the task is same as **Simple Project 1** but only differece is that we are putting different **Grade** data in different excel spreadsheet. 34 | 35 | ## Requirements 36 | python 37 | pandas 38 | openpyxl 39 | 40 | 41 | ## Reference 42 | https://openpyxl.readthedocs.io/en/stable/ 43 | https://zetcode.com/python/openpyxl/ 44 | 45 | -------------------------------------------------------------------------------- /Excel_Operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Excel Automation with Python" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Table of Content: \n", 15 | " 1. Write data using openpyxl\n", 16 | " 2. Write data using pandas\n", 17 | " 3. Read data using openpyxl\n", 18 | " 4. Read data using pandas\n", 19 | " 5. Creating Multiple Sheets in master excel file\n", 20 | " 6. Combining multiple excel speadsheet into a master excel\n", 21 | " 7. Plotting bar graph \n", 22 | " 8. Plotting pie graph\n", 23 | " 9. Sample Project 1 \n", 24 | " 10. Sample Project 2" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "### 1. Writting data into xlsx file using **openpyxl**" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "from openpyxl import Workbook \n", 41 | "wb = Workbook() \n", 42 | "sheet = wb.active \n", 43 | "\n", 44 | "sheet['A1'] =\"Roll\" \n", 45 | "sheet['A2'] = 1 \n", 46 | "sheet['A3'] = 2 \n", 47 | "sheet['A4'] = 3\n", 48 | "\n", 49 | "sheet['B1'] =\"Name\" \n", 50 | "sheet['B2'] = \"Mickel\" \n", 51 | "sheet['B3'] = \"Ronit\" \n", 52 | "sheet['B4'] = \"Rohit\"\n", 53 | "\n", 54 | "sheet['C1'] =\"Marks\" \n", 55 | "sheet['C2'] = 80 \n", 56 | "sheet['C3'] = 68 \n", 57 | "sheet['C4'] = 39\n", 58 | " \n", 59 | "wb.save(\"Data/Student_Record_1.xlsx\")" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "### 2. Writting data into xlsx file using **Pandas**" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "import pandas as pd\n", 76 | "\n", 77 | "Roll = [1, 2, 3]\n", 78 | "Name = ['Mickel', 'Ronit', 'Rohit']\n", 79 | "Marks = [80, 68, 39]\n", 80 | "\n", 81 | "df = pd.DataFrame({'Name':Name, 'Roll':Roll, 'Marks': Marks})\n", 82 | "\n", 83 | "# Create a Pandas Excel writer using XlsxWriter as the engine.\n", 84 | "writer = pd.ExcelWriter('Data/Student_Record_2.xlsx')\n", 85 | "\n", 86 | "# Convert the dataframe to an XlsxWriter Excel object.\n", 87 | "df.to_excel(writer, sheet_name='sheet',header=True, index=False)\n", 88 | "\n", 89 | "# Close the Pandas Excel writer and output the Excel file.\n", 90 | "writer.save()" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "### 3. Reading data from xlsx file using openpyxl" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "['Roll', 'Name', 'Marks']\n", 110 | "[1, 'Mickel', 80]\n", 111 | "[2, 'Ronit', 68]\n", 112 | "[3, 'Rohit', 39]\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "import openpyxl\n", 118 | "path = \"Data/Student_Record_1.xlsx\"\n", 119 | "wb = openpyxl.load_workbook(path)\n", 120 | "sheet = wb.active\n", 121 | "for row in sheet:\n", 122 | " print([data.value for data in row])" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### 4. Reading data from xlsx file using pandas" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 5, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | " Name Roll Marks\n", 142 | "0 Mickel 1 80\n", 143 | "1 Ronit 2 68\n", 144 | "2 Rohit 3 39\n" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "import pandas as pd\n", 150 | "df = pd.read_excel(\"Data/Student_Record_2.xlsx\", sheet_name='sheet1')\n", 151 | "print(df)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "### 5. Creating Mutipliple Sheet in single excel spreadsheet" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 8, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "Before Creating: ['Sheet']\n", 171 | "After Creating: ['Sheet', 'Student']\n", 172 | "After Creating: ['Teacher', 'Sheet', 'Student']\n", 173 | "After Deleting: ['Teacher', 'Student']\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "import openpyxl\n", 179 | "\n", 180 | "book = openpyxl.load_workbook('Data/Sample_file.xlsx')\n", 181 | "print(\"Before Creating: \",book.sheetnames)\n", 182 | "\n", 183 | "#Creating new sheets in Sample_file.xlsx\n", 184 | "book.create_sheet(\"Student\")\n", 185 | "print(\"After Creating: \",book.sheetnames)\n", 186 | "\n", 187 | "# creating new sheet (\"Sheet_name\",Position) \n", 188 | "book.create_sheet(\"Teacher\", 0)\n", 189 | "print(\"After Creating: \",book.sheetnames)\n", 190 | "\n", 191 | "# Delete sheet from file\n", 192 | "book.remove(book['Sheet'])\n", 193 | "print(\"After Deleting: \",book.sheetnames)\n", 194 | "\n", 195 | "book.save('Data/Sample_file.xlsx')" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "### 6. Combining multiple excel speadsheet into a master excel" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 9, 208 | "metadata": {}, 209 | "outputs": [], 210 | "source": [ 211 | "# importing the module\n", 212 | "import pandas\n", 213 | " \n", 214 | "# reading the files\n", 215 | "df1 = pandas.read_excel(\"Data/Student_details.xlsx\")\n", 216 | "df2 = pandas.read_excel(\"Data/Marks.xlsx\")\n", 217 | " \n", 218 | "# merging the files\n", 219 | "df3 = df1[[\"Roll No\",\"Name\",\"Email id\"]].merge(df2[[\"Roll No\",\"English\", \"Science\", \"History\"]], on = \"Roll No\", how = \"left\")\n", 220 | " \n", 221 | "# creating a new file\n", 222 | "df3.to_excel(\"Data/Student_details_with_marks.xlsx\", index = False)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "### 7. Drawing Bar graph using Openpyxl" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 10, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "import openpyxl\n", 239 | "from openpyxl.chart import BarChart, Series, Reference\n", 240 | "\n", 241 | "path = \"Data/Mark.xlsx\"\n", 242 | "wb = openpyxl.load_workbook(path)\n", 243 | "ws = wb.active\n", 244 | "\n", 245 | "chart1 = BarChart()\n", 246 | "chart1.type = \"col\"\n", 247 | "chart1.style = 10\n", 248 | "chart1.title = \"Bar Chart\"\n", 249 | "chart1.y_axis.title = 'Marks Obtain'\n", 250 | "chart1.x_axis.title = 'Roll No'\n", 251 | "\n", 252 | "data = Reference(ws, min_col=2, min_row=1, max_row=11, max_col=4)\n", 253 | "cats = Reference(ws, min_col=1, min_row=2, max_row=11)\n", 254 | "chart1.add_data(data, titles_from_data=True)\n", 255 | "chart1.set_categories(cats)\n", 256 | "chart1.shape = 4\n", 257 | "ws.add_chart(chart1, \"A10\")\n", 258 | "\n", 259 | "wb.save(\"Data/Mark.xlsx\")" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "## 8. Drawing Pie Graph using openpyxl" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 12, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [ 275 | "import openpyxl\n", 276 | "\n", 277 | "from openpyxl.chart import (\n", 278 | " PieChart,\n", 279 | " ProjectedPieChart,\n", 280 | " Reference\n", 281 | ")\n", 282 | "from openpyxl.chart.series import DataPoint\n", 283 | "\n", 284 | "path = \"Data/pie.xlsx\"\n", 285 | "wb = openpyxl.load_workbook(path)\n", 286 | "ws = wb.active\n", 287 | "\n", 288 | "pie = PieChart()\n", 289 | "labels = Reference(ws, min_col=1, min_row=2, max_row=8)\n", 290 | "data = Reference(ws, min_col=2, min_row=1, max_row=8)\n", 291 | "pie.add_data(data, titles_from_data=True)\n", 292 | "pie.set_categories(labels)\n", 293 | "pie.title = \"Monthly Salary Expense\"\n", 294 | "\n", 295 | "# Cut the first slice out of the pie\n", 296 | "slice = DataPoint(idx=0, explosion=20)\n", 297 | "pie.series[0].data_points = [slice]\n", 298 | "\n", 299 | "ws.add_chart(pie, \"D1\")\n", 300 | "\n", 301 | "wb.save(\"Data/pie.xlsx\")" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "### 9. Simple Project 1\n", 309 | "Task 1: we are going to add two more column in excel spreadsheet **Result** and **Grade**.\n", 310 | "\n", 311 | "> In **Result** column we will put **pass** and **Failed** based on Percentage He/She got.
\n", 312 | "1. **Pass**: If percentage is greater than and equal to 40.\n", 313 | "2. **Failed**: If percentage is less than 40\n", 314 | "\n", 315 | "> In **Grade** column we will put **1st class**, **2nd class**, **3rd class** and **Failed** based on Percentage He/She got.\n", 316 | "1. 1st class: If percentage greater than equal to 60.\n", 317 | "2. 2nd class: If percentage greater than equal to 50 but less than 60.\n", 318 | "3. 3rd class: If percentage greater than equal to 40 but less than 50.\n", 319 | "4. Failed: If percentage less than 40.\n", 320 | " \n", 321 | "Task 2: After doing the first task we will create multiple sheet in the same excel file base on Grade He/She got." 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 13, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/html": [ 332 | "
\n", 333 | "\n", 346 | "\n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | "
RollNameEmailGenderPercentage
01Ashaasha@rs.comFemal58
12Sonalsonal@rs.comFemal25
23Krishankrishan@rs.comMale95
34Sonalisonali@rs.comFemal45
45Rituritu@rs.comFemal67
\n", 400 | "
" 401 | ], 402 | "text/plain": [ 403 | " Roll Name Email Gender Percentage\n", 404 | "0 1 Asha asha@rs.com Femal 58\n", 405 | "1 2 Sonal sonal@rs.com Femal 25\n", 406 | "2 3 Krishan krishan@rs.com Male 95\n", 407 | "3 4 Sonali sonali@rs.com Femal 45\n", 408 | "4 5 Ritu ritu@rs.com Femal 67" 409 | ] 410 | }, 411 | "execution_count": 13, 412 | "metadata": {}, 413 | "output_type": "execute_result" 414 | } 415 | ], 416 | "source": [ 417 | "# importing required library\n", 418 | "import pandas as pd\n", 419 | "import xlrd\n", 420 | "import openpyxl\n", 421 | "from openpyxl import Workbook\n", 422 | "df = pd.read_excel(\"Data/student_result.xlsx\", sheet_name='Sheet1')\n", 423 | "df.head()" 424 | ] 425 | }, 426 | { 427 | "cell_type": "markdown", 428 | "metadata": {}, 429 | "source": [ 430 | "### Performing Task 1 (Adding Result column)" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 14, 436 | "metadata": {}, 437 | "outputs": [], 438 | "source": [ 439 | "wb= openpyxl.load_workbook(\"Data/student_result.xlsx\")\n", 440 | "sheet = wb['Sheet1']\n", 441 | "rows_max=sheet.max_row\n", 442 | "sheet.cell(row=1, column=6).value =\"Result\"\n", 443 | "for i in range(2,sheet.max_row + 1):\n", 444 | " if sheet.cell(row=i, column=5).value >= 40:\n", 445 | " sheet.cell(row=i, column=6).value = \"Pass\"\n", 446 | " else:\n", 447 | " sheet.cell(row=i, column=6).value =\"Failed\"\n", 448 | " \n", 449 | "wb.save(\"Data/student_result.xlsx\")" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 15, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/html": [ 460 | "
\n", 461 | "\n", 474 | "\n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | "
RollNameEmailGenderPercentageResult
01Ashaasha@rs.comFemal58Pass
12Sonalsonal@rs.comFemal25Failed
23Krishankrishan@rs.comMale95Pass
34Sonalisonali@rs.comFemal45Pass
45Rituritu@rs.comFemal67Pass
\n", 534 | "
" 535 | ], 536 | "text/plain": [ 537 | " Roll Name Email Gender Percentage Result\n", 538 | "0 1 Asha asha@rs.com Femal 58 Pass\n", 539 | "1 2 Sonal sonal@rs.com Femal 25 Failed\n", 540 | "2 3 Krishan krishan@rs.com Male 95 Pass\n", 541 | "3 4 Sonali sonali@rs.com Femal 45 Pass\n", 542 | "4 5 Ritu ritu@rs.com Femal 67 Pass" 543 | ] 544 | }, 545 | "execution_count": 15, 546 | "metadata": {}, 547 | "output_type": "execute_result" 548 | } 549 | ], 550 | "source": [ 551 | "df = pd.read_excel(\"Data/student_result.xlsx\", sheet_name='Sheet1')\n", 552 | "df.head()" 553 | ] 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "metadata": {}, 558 | "source": [ 559 | "### Performing Task 1 (Adding Grade column )" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 16, 565 | "metadata": {}, 566 | "outputs": [], 567 | "source": [ 568 | "wb= openpyxl.load_workbook(\"Data/student_result.xlsx\")\n", 569 | "sheet = wb['Sheet1']\n", 570 | "rows_max=sheet.max_row\n", 571 | "sheet.cell(row=1, column=7).value =\"Grades\"\n", 572 | "for i in range(2,sheet.max_row + 1):\n", 573 | " if sheet.cell(row=i, column=5).value >= 60:\n", 574 | " sheet.cell(row=i, column=7).value = \"1st class\"\n", 575 | " \n", 576 | " elif sheet.cell(row=i, column=5).value <= 60 and sheet.cell(row=i, column=5).value >=50 :\n", 577 | " sheet.cell(row=i, column=7).value = \"2nd class\"\n", 578 | " \n", 579 | " elif sheet.cell(row=i, column=5).value <= 50 and sheet.cell(row=i, column=5).value >=40 :\n", 580 | " sheet.cell(row=i, column=7).value = \"3rd class\"\n", 581 | " else:\n", 582 | " sheet.cell(row=i, column=7).value =\"Failed\"\n", 583 | " \n", 584 | "wb.save(\"Data/student_result.xlsx\")" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 17, 590 | "metadata": {}, 591 | "outputs": [ 592 | { 593 | "data": { 594 | "text/html": [ 595 | "
\n", 596 | "\n", 609 | "\n", 610 | " \n", 611 | " \n", 612 | " \n", 613 | " \n", 614 | " \n", 615 | " \n", 616 | " \n", 617 | " \n", 618 | " \n", 619 | " \n", 620 | " \n", 621 | " \n", 622 | " \n", 623 | " \n", 624 | " \n", 625 | " \n", 626 | " \n", 627 | " \n", 628 | " \n", 629 | " \n", 630 | " \n", 631 | " \n", 632 | " \n", 633 | " \n", 634 | " \n", 635 | " \n", 636 | " \n", 637 | " \n", 638 | " \n", 639 | " \n", 640 | " \n", 641 | " \n", 642 | " \n", 643 | " \n", 644 | " \n", 645 | " \n", 646 | " \n", 647 | " \n", 648 | " \n", 649 | " \n", 650 | " \n", 651 | " \n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | "
RollNameEmailGenderPercentageResultGrades
01Ashaasha@rs.comFemal58Pass2nd class
12Sonalsonal@rs.comFemal25FailedFailed
23Krishankrishan@rs.comMale95Pass1st class
34Sonalisonali@rs.comFemal45Pass3rd class
45Rituritu@rs.comFemal67Pass1st class
\n", 675 | "
" 676 | ], 677 | "text/plain": [ 678 | " Roll Name Email Gender Percentage Result Grades\n", 679 | "0 1 Asha asha@rs.com Femal 58 Pass 2nd class\n", 680 | "1 2 Sonal sonal@rs.com Femal 25 Failed Failed\n", 681 | "2 3 Krishan krishan@rs.com Male 95 Pass 1st class\n", 682 | "3 4 Sonali sonali@rs.com Femal 45 Pass 3rd class\n", 683 | "4 5 Ritu ritu@rs.com Femal 67 Pass 1st class" 684 | ] 685 | }, 686 | "execution_count": 17, 687 | "metadata": {}, 688 | "output_type": "execute_result" 689 | } 690 | ], 691 | "source": [ 692 | "df = pd.read_excel(\"Data/student_result.xlsx\", sheet_name='Sheet1')\n", 693 | "df.head()" 694 | ] 695 | }, 696 | { 697 | "cell_type": "markdown", 698 | "metadata": {}, 699 | "source": [ 700 | "### Sperating into multiple sheet in same excel file based on the grade obtain." 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": 18, 706 | "metadata": {}, 707 | "outputs": [], 708 | "source": [ 709 | "import pandas as pd\n", 710 | "import xlrd\n", 711 | "import openpyxl\n", 712 | "from openpyxl import Workbook\n", 713 | "wb= openpyxl.load_workbook(\"Data/student_result.xlsx\")\n", 714 | "sheet = wb['Sheet1']\n", 715 | "\n", 716 | "def multiple_sheet(t1,grade):\n", 717 | " work_sheet = wb.create_sheet(title=grade)\n", 718 | " for row in t1:\n", 719 | " work_sheet.append(row)\n", 720 | " wb.save(\"Data/student_result.xlsx\")\n", 721 | "\n", 722 | "def grade_wise(row_num,grade): \n", 723 | " t1=[]\n", 724 | " for j in row_num:\n", 725 | " t=[]\n", 726 | " for col in sheet.iter_cols(min_col=1,max_col=sheet.max_column, min_row = j,max_row=j ):\n", 727 | " for cell in col:\n", 728 | " t.append(cell.value)\n", 729 | " t1.append(tuple(t)) \n", 730 | " multiple_sheet(t1,grade)\n", 731 | " \n", 732 | "def separating():\n", 733 | " df = pd.read_excel(\"Data/student_result.xlsx\", sheet_name='Sheet1')\n", 734 | " grades=[]\n", 735 | " for i in range(len(df[\"Grades\"])):\n", 736 | " rec=df[\"Grades\"][i]\n", 737 | " grades.append(rec) \n", 738 | " grades=list(set(grades))\n", 739 | " start = 1\n", 740 | " cols_max=sheet.max_column\n", 741 | " rows_max=sheet.max_row\n", 742 | "\n", 743 | " for grade in grades:\n", 744 | " row_col=[]\n", 745 | " row_col.append(1)\n", 746 | " for col in sheet.iter_cols(min_col=cols_max, min_row = start, max_row=rows_max):\n", 747 | " for cell in col:\n", 748 | " if cell.value == grade:\n", 749 | " row_col.append(cell.row)\n", 750 | " grade_wise(row_col,grade) \n", 751 | " \n", 752 | "separating()" 753 | ] 754 | }, 755 | { 756 | "cell_type": "markdown", 757 | "metadata": {}, 758 | "source": [ 759 | "## 10. Simple Project 2\n", 760 | "In this project we are separating the records in different excel spreadsheet for different **Grade**" 761 | ] 762 | }, 763 | { 764 | "cell_type": "code", 765 | "execution_count": 20, 766 | "metadata": {}, 767 | "outputs": [], 768 | "source": [ 769 | "import pandas as pd\n", 770 | "import xlrd\n", 771 | "import openpyxl\n", 772 | "from openpyxl import Workbook\n", 773 | "\n", 774 | "def separate_excel_file(t1,grade):\n", 775 | " book = Workbook()\n", 776 | " sheet = book.active\n", 777 | " for row in t1:\n", 778 | " sheet.append(row)\n", 779 | " book.save('output/'+grade+'.xlsx')\n", 780 | "\n", 781 | "def grade_wise(row_num,grade): \n", 782 | " t1=[]\n", 783 | " for j in row_num:\n", 784 | " t=[]\n", 785 | " for col in sheet.iter_cols(min_col=1,max_col=sheet.max_column, min_row = j,max_row=j ):\n", 786 | " for cell in col:\n", 787 | " t.append(cell.value) \n", 788 | " t1.append(tuple(t)) \n", 789 | " separate_excel_file(t1,grade)\n", 790 | " \n", 791 | "def separating():\n", 792 | " df = pd.read_excel(\"Data/student_result.xlsx\", sheet_name='Sheet1')\n", 793 | " grades=[]\n", 794 | " for i in range(len(df[\"Grades\"])):\n", 795 | " rec=df[\"Grades\"][i]\n", 796 | " grades.append(rec) \n", 797 | " grades=list(set(grades))\n", 798 | " start = 1\n", 799 | " cols_max=sheet.max_column\n", 800 | " rows_max=sheet.max_row\n", 801 | " for grade in grades:\n", 802 | " row_col=[]\n", 803 | " row_col.append(1)\n", 804 | " for col in sheet.iter_cols(min_col=cols_max, min_row = start, max_row=rows_max):\n", 805 | " for cell in col:\n", 806 | " if cell.value == grade:\n", 807 | " row_col.append(cell.row)\n", 808 | " grade_wise(row_col,grade) \n", 809 | " \n", 810 | " \n", 811 | "separating()" 812 | ] 813 | }, 814 | { 815 | "cell_type": "markdown", 816 | "metadata": {}, 817 | "source": [ 818 | "## Reference\n", 819 | " https://openpyxl.readthedocs.io/en/stable/\n", 820 | " https://zetcode.com/python/openpyxl/" 821 | ] 822 | }, 823 | { 824 | "cell_type": "code", 825 | "execution_count": null, 826 | "metadata": {}, 827 | "outputs": [], 828 | "source": [] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": null, 833 | "metadata": {}, 834 | "outputs": [], 835 | "source": [] 836 | } 837 | ], 838 | "metadata": { 839 | "kernelspec": { 840 | "display_name": "Python 3", 841 | "language": "python", 842 | "name": "python3" 843 | }, 844 | "language_info": { 845 | "codemirror_mode": { 846 | "name": "ipython", 847 | "version": 3 848 | }, 849 | "file_extension": ".py", 850 | "mimetype": "text/x-python", 851 | "name": "python", 852 | "nbconvert_exporter": "python", 853 | "pygments_lexer": "ipython3", 854 | "version": "3.7.4" 855 | } 856 | }, 857 | "nbformat": 4, 858 | "nbformat_minor": 4 859 | } 860 | --------------------------------------------------------------------------------