{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 7. Exercise Solutions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 1 (Setup)\n",
"\n",
"After running `xlwings quickstart file_name`, the generated file has the following contents: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import xlwings as xw\n",
"\n",
"@xw.sub # only required if you want to import it or run it via UDF Server\n",
"def main():\n",
" wb = xw.Book.caller()\n",
" wb.sheets[0].range(\"A1\").value = \"Hello xlwings!\"\n",
"\n",
"\n",
"@xw.func\n",
"def hello(name):\n",
" return \"hello {0}\".format(name)\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" xw.books.active.set_mock_caller()\n",
" main()\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2 and 3\n",
"\n",
"After solving Exercise 2 and 3, the Python file could look like this:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Standard library imports\n",
"import os\n",
"\n",
"# Third party imports (dependencies)\n",
"import xlwings as xw\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"@xw.sub # only required if you want to import it or run it via UDF Server\n",
"def main():\n",
" wb = xw.Book.caller()\n",
" wb.sheets[0].range(\"A1\").value = \"Hello xlwings!\"\n",
"\n",
"\n",
"# UDF function that was generated here has been deleted (not used)\n",
"\n",
"\n",
"# -----------------------------------------------------------------------------\n",
"# EXERCISE 3\n",
"# ----------\n",
"def create_daterange_dataframe(filename, start_date, end_date):\n",
" '''Helper function'''\n",
"\n",
" # Get the 'correct' current working dir (where the .py and .xlsm files are)\n",
" cwd = os.path.dirname(os.path.abspath(__file__))\n",
"\n",
" # Combine into a full path to the dataset file\n",
" full_filename = f'{cwd}\\\\{filename}'\n",
"\n",
" df_raw = pd.read_csv(full_filename)\n",
"\n",
" # Change the \"raw\" dates into datetime objects so they can be filtered\n",
" df_raw['Date'] = pd.to_datetime(df_raw['Date'])\n",
" \n",
" # Set the date as the index of the dataframe\n",
" df = df_raw.set_index('Date')\n",
"\n",
" # Filter dataframe for input date range and return it\n",
" return df[start_date:end_date]\n",
"\n",
"\n",
"@xw.sub\n",
"def update_weather_plot():\n",
" '''Macro function to interact with Excel'''\n",
"\n",
" # Mock the calling Excel file\n",
" xw.books.active.set_mock_caller()\n",
" \n",
" # Get the workbook that is calling the macro and set sheet\n",
" wb = xw.Book.caller()\n",
" sht3 = wb.sheets['Sheet3']\n",
"\n",
" # Get input values from Excel\n",
" start_date = sht3.range('C21').value\n",
" end_date = sht3.range('C22').value\n",
" parameter = sht3.range('C23').value\n",
"\n",
" fig, ax = plt.subplots()\n",
"\n",
" filename = 'Sydney_weather.csv'\n",
"\n",
" # Create a dataframe from the dataset and filter it based on inputs\n",
" df = create_daterange_dataframe(filename, start_date, end_date)\n",
"\n",
" # Use the Pandas interface to matplotlib for plotting\n",
" df.plot(y=parameter, ax=ax)\n",
"\n",
" # Insert figure into sheet\n",
" sht3.pictures.add(fig, name='MyPlot', update=True)\n",
" \n",
"# -----------------------------------------------------------------------------\n",
"\n",
" \n",
"if __name__ == \"__main__\":\n",
" xw.books.active.set_mock_caller()\n",
" main()\n",
"\n",
" # -------------------------------------------------------------------------\n",
" # EXERCISE 2\n",
" # ----------\n",
" import numpy as np\n",
"\n",
" # Establish connection to the workbook\n",
" wb = xw.Book.caller()\n",
" \n",
" sht2 = wb.sheets['Sheet2']\n",
" \n",
" sht2.range('A2').options(transpose=True).value = np.arange(-50, 51, 5)\n",
"\n",
" # -------------------------------------------------------------------------\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Look of resulting Excel interface\n",
"\n",
"The look of the resulting Excel interface could be like shown below:\n",
"\n",
"
\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Demonstration of `pd.describe`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
| \n", " | MinTemp | \n", "MaxTemp | \n", "Rainfall | \n", "Evaporation | \n", "Sunshine | \n", "WindGustSpeed | \n", "WindSpeed9am | \n", "WindSpeed3pm | \n", "Humidity9am | \n", "Humidity3pm | \n", "Pressure9am | \n", "Pressure3pm | \n", "Cloud9am | \n", "Cloud3pm | \n", "Temp9am | \n", "Temp3pm | \n", "RISK_MM | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| count | \n", "3334.000000 | \n", "3335.000000 | \n", "3331.000000 | \n", "3286.000000 | \n", "3321.000000 | \n", "2301.000000 | \n", "3311.000000 | \n", "3312.000000 | \n", "3323.000000 | \n", "3324.000000 | \n", "3317.000000 | \n", "3318.000000 | \n", "2771.000000 | \n", "2776.000000 | \n", "3333.000000 | \n", "3333.000000 | \n", "3337.000000 | \n", "
| mean | \n", "14.865057 | \n", "23.002339 | \n", "3.330231 | \n", "5.187432 | \n", "7.179374 | \n", "41.761408 | \n", "15.070674 | \n", "19.335447 | \n", "68.229010 | \n", "54.699158 | \n", "1018.346156 | \n", "1016.018774 | \n", "4.181523 | \n", "4.218660 | \n", "17.819742 | \n", "21.533333 | \n", "3.319868 | \n", "
| std | \n", "4.553641 | \n", "4.494638 | \n", "9.895172 | \n", "2.777407 | \n", "3.810886 | \n", "13.007523 | \n", "7.079724 | \n", "7.482554 | \n", "15.085355 | \n", "16.293530 | \n", "7.021571 | \n", "7.032211 | \n", "2.749578 | \n", "2.641885 | \n", "4.897177 | \n", "4.303737 | \n", "9.885066 | \n", "
| min | \n", "4.300000 | \n", "11.700000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "17.000000 | \n", "0.000000 | \n", "0.000000 | \n", "19.000000 | \n", "10.000000 | \n", "986.700000 | \n", "989.800000 | \n", "0.000000 | \n", "0.000000 | \n", "6.400000 | \n", "10.200000 | \n", "0.000000 | \n", "
| 25% | \n", "11.000000 | \n", "19.600000 | \n", "0.000000 | \n", "3.200000 | \n", "4.300000 | \n", "31.000000 | \n", "9.000000 | \n", "15.000000 | \n", "58.000000 | \n", "44.000000 | \n", "1013.700000 | \n", "1011.300000 | \n", "1.000000 | \n", "1.000000 | \n", "13.800000 | \n", "18.400000 | \n", "0.000000 | \n", "
| 50% | \n", "14.900000 | \n", "22.800000 | \n", "0.000000 | \n", "4.800000 | \n", "8.300000 | \n", "41.000000 | \n", "15.000000 | \n", "19.000000 | \n", "69.000000 | \n", "56.000000 | \n", "1018.600000 | \n", "1016.300000 | \n", "5.000000 | \n", "4.000000 | \n", "18.200000 | \n", "21.300000 | \n", "0.000000 | \n", "
| 75% | \n", "18.700000 | \n", "26.000000 | \n", "1.400000 | \n", "7.000000 | \n", "10.200000 | \n", "50.000000 | \n", "20.000000 | \n", "24.000000 | \n", "80.000000 | \n", "65.000000 | \n", "1023.100000 | \n", "1020.800000 | \n", "7.000000 | \n", "7.000000 | \n", "21.700000 | \n", "24.500000 | \n", "1.400000 | \n", "
| max | \n", "27.600000 | \n", "45.800000 | \n", "119.400000 | \n", "18.400000 | \n", "13.600000 | \n", "96.000000 | \n", "54.000000 | \n", "57.000000 | \n", "100.000000 | \n", "99.000000 | \n", "1039.000000 | \n", "1036.700000 | \n", "9.000000 | \n", "8.000000 | \n", "36.500000 | \n", "44.700000 | \n", "119.400000 | \n", "