Skip to content

Commit 293e371

Browse files
committed
added HTML to Excel conversion
1 parent 6ef5d98 commit 293e371

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# HTML file into a CSV file
2+
This script is designed to convert data from an HTML file into a CSV file and then modify that CSV file in Excel format.
3+
4+
## Requirements
5+
1. Python 3.x
6+
2. openpyxl
7+
3. pandas
8+
## Usage
9+
1. Ensure that the required packages are installed.
10+
2. Update the file paths in the script to reflect your specific file names and paths.
11+
3. Run the script from the command line: python script_name.py.
12+
4. The script will perform the following tasks:
13+
5. Convert the data from the HTML file to a CSV file.
14+
6. Modify the CSV file in Excel format by bolding the font of certain cells.
15+
7. Once the script is complete, the modified Excel file will be saved in the specified file path.
16+
17+
Note: Ensure that the HTML file is in the same directory as the script or specify the correct path in the script.
18+

0 commit comments

Comments
 (0)