|
| 1 | +# Importing necessary libraries |
| 2 | +import csv |
| 3 | +import openpyxl |
| 4 | +import pandas as pd |
| 5 | +from openpyxl import Workbook |
| 6 | +from openpyxl.styles import Font |
| 7 | + |
| 8 | +# Reading HTML file and defining paths for CSV and Excel files |
| 9 | +file = pd.read_html("./Test Report_2021-08-18_12-45-00.html") |
| 10 | +path = "./your_csv_name.csv" |
| 11 | +xlpath = 'name.xlsx' |
| 12 | + |
| 13 | +# Function to write data from HTML to CSV and convert it to Excel format |
| 14 | +def write_html_csv(): |
| 15 | + for index, data in enumerate(file): |
| 16 | + # Check for index value and print data |
| 17 | + if index: |
| 18 | + data.to_csv("./your_csv_name.csv", mode='a+', header=True) |
| 19 | + |
| 20 | + # Creating an instance of Workbook and creating a new sheet |
| 21 | + wb = Workbook() |
| 22 | + ws = wb.active |
| 23 | + |
| 24 | + # Reading CSV file and writing data to Excel |
| 25 | + with open(path, 'r') as f: |
| 26 | + for row in csv.reader(f): |
| 27 | + ws.append(row) |
| 28 | + |
| 29 | + # Saving the Excel file |
| 30 | + wb.save(xlpath) |
| 31 | + |
| 32 | +# Function to modify the Excel sheet by adding bold font to certain cell values |
| 33 | +def modify_excel(): |
| 34 | + # Opening the Excel file |
| 35 | + wb_obj = openpyxl.load_workbook(xlpath) |
| 36 | + sheet_obj = wb_obj.active |
| 37 | + |
| 38 | + # Getting the number of rows and columns in the sheet |
| 39 | + rows = sheet_obj.max_row |
| 40 | + cols = sheet_obj.max_column |
| 41 | + |
| 42 | + # Looping through each cell and checking for certain values to apply font style |
| 43 | + for i in range(1, rows + 1): |
| 44 | + for j in range(1, cols + 1): |
| 45 | + if ("Test_Cases" in str(sheet_obj.cell(i, j).value)) or ("Status" in str(sheet_obj.cell(i, j).value)): |
| 46 | + x = sheet_obj.cell(i, j).coordinate |
| 47 | + y = sheet_obj.cell(i, j).row |
| 48 | + sheet_obj[x].font = Font(bold=True) |
| 49 | + |
| 50 | + # Saving the modified Excel file |
| 51 | + wb_obj.save(xlpath) |
| 52 | + |
| 53 | +# Running the functions and printing messages to indicate completion of tasks |
| 54 | +print("Starting task one") |
| 55 | +write_html_csv() |
| 56 | +print("Task one over") |
| 57 | +print("Starting task two") |
| 58 | +modify_excel() |
| 59 | +print("Task two over") |
0 commit comments