From bf9e80393a8d64608047f08706a5ed01b3bbe392 Mon Sep 17 00:00:00 2001 From: Nico Melone Date: Wed, 13 Nov 2024 17:46:35 -0600 Subject: [PATCH] updates to the ekko report sandbox --- tb_report/backend/buildReportEKKO.ipynb | 48 +++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/tb_report/backend/buildReportEKKO.ipynb b/tb_report/backend/buildReportEKKO.ipynb index b46a1af..ac0cd9a 100644 --- a/tb_report/backend/buildReportEKKO.ipynb +++ b/tb_report/backend/buildReportEKKO.ipynb @@ -8,8 +8,7 @@ "source": [ "import logging, json, boto3, pytz, math, os, shutil\n", "from openpyxl.utils import get_column_letter\n", - "from openpyxl.utils.datetime import from_ISO8601\n", - "from openpyxl.worksheet.table import Table\n", + "from openpyxl.utils.datetime import CALENDAR_WINDOWS_1900, to_excel\n", "import pandas as pd\n", "from datetime import datetime as dt\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", " # Set 'ts' as the index\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", " temp_df.rename(columns={'value': formatColumnName(datapoint)}, inplace=True)\n", " \n", @@ -249,6 +249,20 @@ " 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", "execution_count": 13, @@ -283,7 +297,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1441\n" + ] + } + ], "source": [ "# 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", @@ -337,7 +359,7 @@ " #df.index = [dt.fromtimestamp(x) for x in df.index ]\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", - " 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", " # Get the xlsxwriter workbook and worksheet objects.\n", " workbook = writer.book\n", @@ -345,9 +367,9 @@ " for row in worksheet.iter_rows(min_row=2, max_col=1):\n", " for cell in row:\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", - " cell.number_format = '#,##0.00'\n", + " cell.number_format = '#,##0.00'\"\"\"\n", " \n", " \n", "\"\"\"\n", @@ -412,6 +434,18 @@ " chartsheet.insert_chart('A' + str(position), chart, {'x_scale': 3, 'y_scale': 2})\n", " position += 30\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", "reportsheet[\"B9\"] = dt.fromtimestamp(getTime(time)[0]/1000).strftime('%m/%d/%Y')\n", "reportsheet[\"B10\"] = \"Test Well Name\"\n", @@ -433,7 +467,7 @@ "metadata": {}, "outputs": [], "source": [ - "type(df.index[0])" + "df" ] }, {