Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add html_2_xlsx notebook and files #25

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions examples/html_2_xlsx.ipynb
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the cambioML notebook ending and slightly more descriptive phrases.

Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HTML 2 XLSX\n",
"Notebook to convert the HTML tables in the output of `AnyParser` to an XLSX file."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Install Libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# !pip3 install BeautifulSoup4\n",
"# !pip3 install lxml\n",
"# !pip3 install openpyxl\n",
"# !pip3 install pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input Data\n",
"First, we load the `input_html` string from the `AnyParser` output, located in the html_2_xlsx/input folder."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from html_2_xlsx.input.html_input import input_html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert HTML to XLSX\n",
"Next, we define the `html_to_excel` function, which reads the html string and converts it to an XLSX file using `BeautifulSoup` and `pandas`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from bs4 import BeautifulSoup\n",
"import pandas as pd\n",
"from io import StringIO\n",
"\n",
"\n",
"def html_to_excel(html_string, output_folder, output_filename):\n",
" if not os.path.exists(output_folder):\n",
" os.makedirs(output_folder)\n",
"\n",
" soup = BeautifulSoup(html_string, 'html.parser')\n",
"\n",
" tables = soup.find_all('table')\n",
"\n",
" dfs = {}\n",
" for i, table in enumerate(tables):\n",
" dfs[f\"Table_{i+1}\"] = pd.read_html(StringIO(str(table)))[0]\n",
"\n",
" output_file = os.path.join(output_folder, output_filename)\n",
" with pd.ExcelWriter(output_file) as writer:\n",
" for name, df in dfs.items():\n",
" df.to_excel(writer, sheet_name=name, index=False)\n",
"\n",
" print(f\"Excel file saved to {output_file}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we run `html_to_excel` to convert the HTML to XLSX."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Excel file saved to html_2_xlsx/output/html_2_excel_output.xlsx\n"
]
}
],
"source": [
"output_file = 'html_2_excel_output.xlsx'\n",
"output_folder = 'html_2_xlsx/output'\n",
"html_to_excel(input_html, output_folder, output_file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## End of the notebook\n",
"\n",
"Check more [case studies](https://www.cambioml.com/blog) of CambioML!\n",
"\n",
"<a href=\"https://www.cambioml.com/\" title=\"Title\">\n",
" <img src=\"./sample_data/cambioml_logo_large.png\" style=\"height: 100px; display: block; margin-left: auto; margin-right: auto;\"/>\n",
"</a>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "open-parser",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
46 changes: 46 additions & 0 deletions examples/html_2_xlsx/input/html_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
input_html = """<table>
<tbody>
<tr><td>Summary </td><td> </td><td> </td><td>Current Month</td><td>- March-2024</td><td> </td><td> </td><td>Three </td><td>Months - </td><td>1/1/2024 to 3/31/2024</td><td> </td><td> </td><td>Twelve </td><td>Months - </td><td>4/1/2023 to 3/31/2024</td><td> </td></tr>
<tr><td> </td><td> </td><td>Number </td><td>of Reporting </td><td>Licensees - </td><td>444 </td><td> </td><td>Number </td><td>of Reporting</td><td>Licensees - 445 </td><td> </td><td> </td><td>Number </td><td>of Reporting</td><td>Licensees </td><td>453 </td></tr>
<tr><td>Unit Description </td><td># Of Loc*</td><td># Of Units</td><td>Win* Amount </td><td>% Chg </td><td>Win Percent</td><td># Of Loc*</td><td>Avg Units</td><td>Win** Amount</td><td>% Chg </td><td>Win Percent</td><td># Of Loc*</td><td>Avg Units</td><td>Win** Amount</td><td>% Chg </td><td>Win Percent</td></tr>
<tr><td>Table, Counter and Card </td><td>Games </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td>Twenty One </td><td>114 </td><td>2,065 </td><td>108,802 </td><td>(18.17) </td><td>13.59 </td><td>114 </td><td>2,063 </td><td>339,520 </td><td>(7.64) </td><td>13.85 </td><td>115 </td><td>2,017 </td><td>1,297,555 </td><td>(3.75) </td><td>14.19 </td></tr>
<tr><td>Craps </td><td>101 </td><td>336 </td><td>41,567 </td><td>2.11 </td><td>16.48 </td><td>102 </td><td>324 </td><td>118,285 </td><td>1.03 </td><td>16.70 </td><td>105 </td><td>319 </td><td>468,742 </td><td>2.60 </td><td>16.82 </td></tr>
<tr><td>Roulette </td><td>100 </td><td>435 </td><td>37,678 </td><td>8.00 </td><td>20.41 </td><td>100 </td><td>441 </td><td>96,295 </td><td>(20.68) </td><td>14.96 </td><td>101 </td><td>434 </td><td>435,203 </td><td>(7.09) </td><td>18.01 </td></tr>
<tr><td>Ultim Texas Hold&#x27;em </td><td>70 </td><td>194 </td><td>17,256 </td><td>(0.22) </td><td>23.08 </td><td>71 </td><td>194 </td><td>51,882 </td><td>0.45 </td><td>24.06 </td><td>72 </td><td>182 </td><td>188,983 </td><td>0.36 </td><td>21.77 </td></tr>
<tr><td>3-Card Poker </td><td>90 </td><td>165 </td><td>9,997 </td><td>(23.54) </td><td>27.97 </td><td>90 </td><td>167 </td><td>29,993 </td><td>(15.13) </td><td>31.02 </td><td>93 </td><td>169 </td><td>119,934 </td><td>(8.19) </td><td>31.53 </td></tr>
<tr><td>Baccarat </td><td>34 </td><td>447 </td><td>114,694 </td><td>75.51 </td><td>18.81 </td><td>34 </td><td>446 </td><td>392,829 </td><td>51.81 </td><td>17.45 </td><td>36 </td><td>405 </td><td>1,622,476 </td><td>39.79 </td><td>17.85 </td></tr>
<tr><td>Mini-Baccarat </td><td>37 </td><td>115 </td><td>4,980 </td><td>(24.61) </td><td>7.65 </td><td>38 </td><td>119 </td><td>21,183 </td><td>(8.28) </td><td>10.14 </td><td>41 </td><td>115 </td><td>88,698 </td><td>(9.65) </td><td>11.85 </td></tr>
<tr><td>Keno </td><td>31 </td><td>41 </td><td>1,487 </td><td>(6.03) </td><td>30.51 </td><td>31 </td><td>41 </td><td>4,148 </td><td>(8.07) </td><td>29.02 </td><td>33 </td><td>42 </td><td>17,648 </td><td>(5.14) </td><td>29.57 </td></tr>
<tr><td>Bingo </td><td>31 </td><td>31 </td><td>6,890 </td><td>1.51 </td><td>30.58 </td><td>31 </td><td>31 </td><td>12,486 </td><td>(5.93) </td><td>23.22 </td><td>34 </td><td>31 </td><td>44,769 </td><td>(3.15) </td><td>21.65 </td></tr>
<tr><td>Let It Ride </td><td>30 </td><td>35 </td><td>1,487 </td><td>(0.12) </td><td>24.21 </td><td>31 </td><td>35 </td><td>4,184 </td><td>(16.04) </td><td>25.66 </td><td>33 </td><td>37 </td><td>16,970 </td><td>(17.15) </td><td>25.37 </td></tr>
<tr><td>Pai Gow </td><td>13 </td><td>25 </td><td>2,164 </td><td>368.92 </td><td>46.16 </td><td>15 </td><td>26 </td><td>2,731 </td><td>19.56 </td><td>19.10 </td><td>16 </td><td>25 </td><td>11,112 </td><td>19.01 </td><td>22.00 </td></tr>
<tr><td>Pai Gow Poker </td><td>76 </td><td>208 </td><td>10,875 </td><td>(6.82) </td><td>20.99 </td><td>76 </td><td>209 </td><td>32,533 </td><td>22.28 </td><td>21.69 </td><td>78 </td><td>208 </td><td>130,222 </td><td>(5.34) </td><td>21.84 </td></tr>
<tr><td>Race Book (1) </td><td>165 </td><td>163 </td><td>2,815 </td><td>(2.94) </td><td>16.52 </td><td>167 </td><td>164 </td><td>7,076 </td><td>(6.26) </td><td>16.41 </td><td>169 </td><td>160 </td><td>29,962 </td><td>(9.65) </td><td>15.64 </td></tr>
<tr><td>Sports Pool (2) </td><td>181 </td><td>181 </td><td>29,762 </td><td>(32.13) </td><td>3.79 </td><td>181 </td><td>180 </td><td>142,411 </td><td>5.06 </td><td>6.25 </td><td>182 </td><td>177 </td><td>488,194 </td><td>5.12 </td><td>6.02 </td></tr>
<tr><td>Card Games </td><td>35 </td><td>454 </td><td>14,664 </td><td>(4.42) </td><td> </td><td>35 </td><td>452 </td><td>39,840 </td><td>(6.00) </td><td> </td><td>38 </td><td>481 </td><td>226,354 </td><td>1.71 </td><td> </td></tr>
<tr><td>Other </td><td> </td><td>153 </td><td>10,773 </td><td>1.63 </td><td>23.76 </td><td> </td><td>164 </td><td>28,620 </td><td>(14.25) </td><td>18.40 </td><td> </td><td>158 </td><td>129,395 </td><td>1.44 </td><td>20.27 </td></tr>
<tr><td>Total </td><td>263 </td><td>5,048 </td><td>415,892 </td><td>2.54 </td><td>13.98 </td><td>263 </td><td>5,056 </td><td>1,324,017 </td><td>6.31 </td><td>14.17 </td><td>266 </td><td>4,960 </td><td>5,316,219 </td><td>7.81 </td><td>14.94 </td></tr>
<tr><td>Slot Machines </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td>1 Cent </td><td>247 </td><td>40,371 </td><td>220,252 </td><td>(23.43) </td><td>8,97 </td><td>248 </td><td>41,784 </td><td>659,530 </td><td>(21.36) </td><td>9.66 </td><td>256 </td><td>43,757 </td><td>2,973,572 </td><td>(15.68) </td><td>9.58 </td></tr>
<tr><td>5 Cent </td><td>90 </td><td>847 </td><td>3,307 </td><td>(8.54) </td><td>6.56 </td><td>94 </td><td>815 </td><td>9,880 </td><td>(5.73) </td><td>6.61 </td><td>104 </td><td>834 </td><td>41,975 </td><td>(1.27) </td><td>5.69 </td></tr>
<tr><td>25 Cent </td><td>163 </td><td>2,692 </td><td>15,903 </td><td>(15.81) </td><td>7.33 </td><td>167 </td><td>2,761 </td><td>46,711 </td><td>(17.96) </td><td>7.99 </td><td>174 </td><td>2,958 </td><td>209,647 </td><td>(12.20) </td><td>7.93 </td></tr>
<tr><td>1 Dollar </td><td>169 </td><td>5,679 </td><td>43,643 </td><td>(26.86) </td><td>6.23 </td><td>171 </td><td>5,873 </td><td>138,351 </td><td>(20.30) </td><td>6.93 </td><td>179 </td><td>6,157 </td><td>602,837 </td><td>(16.78) </td><td>6.43 </td></tr>
<tr><td>5 Dollars </td><td>87 </td><td>574 </td><td>4,988 </td><td>(20.77) </td><td>5.15 </td><td>88 </td><td>599 </td><td>15,291 </td><td>(23.43) </td><td>5.11 </td><td>92 </td><td>621 </td><td>65,712 </td><td>(22.31) </td><td>5.06 </td></tr>
<tr><td>25 Dollars </td><td>46 </td><td>109 </td><td>1,329 </td><td>(39.77) </td><td>5.90 </td><td>47 </td><td>113 </td><td>4,796 </td><td>(22.22) </td><td>6.39 </td><td>52 </td><td>121 </td><td>21,451 </td><td>(27.94) </td><td>5.21 </td></tr>
<tr><td>100 Dollars </td><td>32 </td><td>86 </td><td>2,196 </td><td>(36.11) </td><td>6.35 </td><td>32 </td><td>87 </td><td>7,722 </td><td>(8.40) </td><td>7.08 </td><td>35 </td><td>86 </td><td>31,216 </td><td>(14.11) </td><td>6.24 </td></tr>
<tr><td>Multi Denomination </td><td>309 </td><td>76,718 </td><td>573,675 </td><td>12.37 </td><td>6.41 </td><td>310 </td><td>75,315 </td><td>1,677,377 </td><td>17.60 </td><td>6.73 </td><td>317 </td><td>70,289 </td><td>6,224,784 </td><td>15.94 </td><td>6.50 </td></tr>
<tr><td>Other </td><td> </td><td>917 </td><td>9,220 </td><td>(34.95) </td><td> </td><td> </td><td>1,009 </td><td>28,173 </td><td>(21.78) </td><td> </td><td> </td><td>1,037 </td><td>124,892 </td><td>(7.27) </td><td> </td></tr>
<tr><td>Total </td><td>316 </td><td>127,993 </td><td>874,515 </td><td>(3.53) </td><td>6.88 </td><td>317 </td><td>128,356 </td><td>2,587,831 </td><td>0.44 </td><td>7.29 </td><td>325 </td><td>125,860 </td><td>10,296,085 </td><td>1.08 </td><td>7.15 </td></tr>
<tr><td>Total Gaming </td><td> </td><td> </td><td>1,290,407 </td><td>(1.65) </td><td> </td><td> </td><td> </td><td>3,911,849 </td><td>2.35 </td><td> </td><td> </td><td> </td><td>15,612,304 </td><td>3.27 </td><td> </td></tr>
<tr><td>(1) Race Book Parimutuel</td><td>60 </td><td>60 </td><td>2,786 </td><td>(3.60) </td><td>16.51 </td><td>62 </td><td>62 </td><td>7,017 </td><td>(6.54) </td><td>16.44 </td><td>66 </td><td>66 </td><td>29,587 </td><td>(8.81) </td><td>15.91 </td></tr>
<tr><td>Sports Mobile </td><td>56 </td><td> </td><td>26,010 </td><td>1.66 </td><td>5.13 </td><td>58 </td><td> </td><td>79,867 </td><td>33.25 </td><td>5.55 </td><td>68 </td><td> </td><td>260,585 </td><td>18.54 </td><td>4.86 </td></tr>
<tr><td>(2) Sports Football </td><td>120 </td><td>120 </td><td>(12,881) </td><td>(3.82) </td><td>(4,049.07) </td><td>181 </td><td>181 </td><td>37,565 </td><td>51.81 </td><td>6.90 </td><td>182 </td><td>182 </td><td>170,755 </td><td>9.25 </td><td>6.42 </td></tr>
<tr><td>Sports Basketball </td><td>180 </td><td>180 </td><td>32,464 </td><td>(27.64) </td><td>5.31 </td><td>180 </td><td>180 </td><td>75,596 </td><td>(7.81) </td><td>5.74 </td><td>182 </td><td>182 </td><td>127,091 </td><td>(4.29) </td><td>5.24 </td></tr>
<tr><td>Sports Baseball </td><td>179 </td><td>179 </td><td>1,974 </td><td> </td><td>5.92 </td><td>179 </td><td>179 </td><td>16 </td><td>(101.44) </td><td>0.05 </td><td>181 </td><td>181 </td><td>94,320 </td><td>17.15 </td><td>5.82 </td></tr>
<tr><td>Sports Parlay Cards </td><td>26 </td><td>26 </td><td>(155) </td><td>(496.78) </td><td>(90.37) </td><td>153 </td><td>153 </td><td>77 </td><td>(96.21) </td><td>2.91 </td><td>163 </td><td>163 </td><td>6,510 </td><td>(43.39) </td><td>23.22 </td></tr>
<tr><td>Sports Hockey </td><td>178 </td><td>178 </td><td>730 </td><td>(85.23) </td><td>1.60 </td><td>178 </td><td>178 </td><td>8,117 </td><td>(34.42) </td><td>6.35 </td><td>181 </td><td>181 </td><td>14,706 </td><td>(41.12) </td><td>3.76 </td></tr>
<tr><td>Other </td><td> </td><td>181 </td><td>7,630 </td><td>4.05 </td><td>8.14 </td><td> </td><td>180 </td><td>21,039 </td><td>35.67 </td><td>8.33 </td><td> </td><td>177 </td><td>74,812 </td><td>28.23 </td><td>7.56 </td></tr>
</tbody>
</table>
"""
Binary file not shown.
Loading