updates to the ekko report sandbox

This commit is contained in:
Nico Melone
2024-11-13 17:46:35 -06:00
parent 18cdc07af3
commit bf9e80393a

View File

@@ -8,8 +8,7 @@
"source": [ "source": [
"import logging, json, boto3, pytz, math, os, shutil\n", "import logging, json, boto3, pytz, math, os, shutil\n",
"from openpyxl.utils import get_column_letter\n", "from openpyxl.utils import get_column_letter\n",
"from openpyxl.utils.datetime import from_ISO8601\n", "from openpyxl.utils.datetime import CALENDAR_WINDOWS_1900, to_excel\n",
"from openpyxl.worksheet.table import Table\n",
"import pandas as pd\n", "import pandas as pd\n",
"from datetime import datetime as dt\n", "from datetime import datetime as dt\n",
"from datetime import timedelta as td\n", "from datetime import timedelta as td\n",
@@ -237,6 +236,7 @@
" temp_df['ts'] = pd.to_datetime(temp_df['ts'], unit='ms').dt.tz_localize('UTC').dt.tz_convert(time[\"timezone\"]).dt.tz_localize(None)\n", " temp_df['ts'] = pd.to_datetime(temp_df['ts'], unit='ms').dt.tz_localize('UTC').dt.tz_convert(time[\"timezone\"]).dt.tz_localize(None)\n",
" # Set 'ts' as the index\n", " # Set 'ts' as the index\n",
" temp_df.set_index('ts', inplace=True)\n", " temp_df.set_index('ts', inplace=True)\n",
" temp_df[\"value\"] = pd.to_numeric(temp_df[\"value\"], errors=\"coerce\")\n",
" # Rename 'value' column to the name of the datapoint\n", " # Rename 'value' column to the name of the datapoint\n",
" temp_df.rename(columns={'value': formatColumnName(datapoint)}, inplace=True)\n", " temp_df.rename(columns={'value': formatColumnName(datapoint)}, inplace=True)\n",
" \n", " \n",
@@ -249,6 +249,20 @@
" return df" " return df"
] ]
}, },
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def get_last_data_row(ws):\n",
" # Start from the bottom row and work up to find the last row with data\n",
" for row in range(ws.max_row, 0, -1):\n",
" if any(cell.value is not None for cell in ws[row]):\n",
" return row\n",
" return 0 # If no data is found, return 0"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 13,
@@ -283,7 +297,15 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1441\n"
]
}
],
"source": [ "source": [
"# Create a Pandas Excel writer using XlsxWriter as the engine.\n", "# Create a Pandas Excel writer using XlsxWriter as the engine.\n",
"shutil.copyfile(\"/Users/nico/Documents/test/ACW Daily Report Template.xlsx\", f\"/Users/nico/Documents/test/Thunderbird_{dt.today().strftime('%Y-%m-%d')}.xlsx\")\n", "shutil.copyfile(\"/Users/nico/Documents/test/ACW Daily Report Template.xlsx\", f\"/Users/nico/Documents/test/Thunderbird_{dt.today().strftime('%Y-%m-%d')}.xlsx\")\n",
@@ -337,7 +359,7 @@
" #df.index = [dt.fromtimestamp(x) for x in df.index ]\n", " #df.index = [dt.fromtimestamp(x) for x in df.index ]\n",
" # Write the dataframe data to XlsxWriter. Turn off the default header and\n", " # Write the dataframe data to XlsxWriter. Turn off the default header and\n",
" # index and skip one row to allow us to insert a user defined header.\n", " # index and skip one row to allow us to insert a user defined header.\n",
" df.to_excel(writer, sheet_name=device, startrow=0, header=True, index=True)\n", " df.to_excel(writer, sheet_name=device, startrow=0, header=True, index=True, float_format=\"%.2f\")\n",
"\n", "\n",
" # Get the xlsxwriter workbook and worksheet objects.\n", " # Get the xlsxwriter workbook and worksheet objects.\n",
" workbook = writer.book\n", " workbook = writer.book\n",
@@ -345,9 +367,9 @@
" for row in worksheet.iter_rows(min_row=2, max_col=1):\n", " for row in worksheet.iter_rows(min_row=2, max_col=1):\n",
" for cell in row:\n", " for cell in row:\n",
" cell.number_format = 'yyyy-mm-dd hh:mm:ss'\n", " cell.number_format = 'yyyy-mm-dd hh:mm:ss'\n",
" for row in worksheet.iter_rows(min_row=2, min_col=2):\n", " \"\"\"for row in worksheet.iter_rows(min_row=2, min_col=2):\n",
" for cell in row:\n", " for cell in row:\n",
" cell.number_format = '#,##0.00'\n", " cell.number_format = '#,##0.00'\"\"\"\n",
" \n", " \n",
" \n", " \n",
"\"\"\"\n", "\"\"\"\n",
@@ -412,6 +434,18 @@
" chartsheet.insert_chart('A' + str(position), chart, {'x_scale': 3, 'y_scale': 2})\n", " chartsheet.insert_chart('A' + str(position), chart, {'x_scale': 3, 'y_scale': 2})\n",
" position += 30\n", " position += 30\n",
"\"\"\"\n", "\"\"\"\n",
"#Getting the data sheet for ACW #1 to access date range actually available\n",
"datasheet = writer.book.worksheets[1]\n",
"datetime_min = datasheet[\"A2\"].value\n",
"last_data_row = get_last_data_row(datasheet)\n",
"datetime_max = datasheet[f\"A{last_data_row}\"].value\n",
"#Convert to excel number\n",
"datetime_min = to_excel(datetime_min)\n",
"datetime_max = to_excel(datetime_max)\n",
"#Change the range of the chart\n",
"chart = reportsheet._charts[0]\n",
"chart.x_axis.scaling.min = datetime_min\n",
"chart.x_axis.scaling.max = datetime_max\n",
"\n", "\n",
"reportsheet[\"B9\"] = dt.fromtimestamp(getTime(time)[0]/1000).strftime('%m/%d/%Y')\n", "reportsheet[\"B9\"] = dt.fromtimestamp(getTime(time)[0]/1000).strftime('%m/%d/%Y')\n",
"reportsheet[\"B10\"] = \"Test Well Name\"\n", "reportsheet[\"B10\"] = \"Test Well Name\"\n",
@@ -433,7 +467,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"type(df.index[0])" "df"
] ]
}, },
{ {