├── .devcontainer ├── Dockerfile ├── devcontainer.json └── startup.sh ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── CONTRIBUTING.md ├── Finished ├── Ch1 │ ├── Inventory.csv │ ├── challenge_solution.py │ ├── read_csv_array.py │ ├── read_csv_dict.py │ ├── read_with_filter.py │ ├── write_csv_array.py │ └── write_csv_dict.py ├── Ch2 │ ├── FinancialSample.xlsx │ ├── Inventory.csv │ ├── cell_styles.py │ ├── challenge_solution.py │ ├── conditional_format.py │ ├── create_filters.py │ ├── create_workbook.py │ ├── explore_workbook.py │ └── workbook_content.py ├── Ch3 │ ├── Inventory.csv │ ├── challenge_solution.py │ ├── xlsx_create.py │ ├── xlsx_format.py │ ├── xlsx_formula_conditional.py │ ├── xlsx_properties.py │ └── xlsx_tables.py └── Ch4 │ ├── FinancialSample.xlsx │ ├── Inventory.csv │ ├── challenge_solution.py │ ├── pandas_explore.py │ ├── pandas_manipulate.py │ ├── pandas_read.py │ └── pandas_write.py ├── LICENSE ├── NOTICE ├── README.md ├── Start ├── Ch1 │ ├── Inventory.csv │ ├── read_csv_array.py │ ├── read_csv_dict.py │ ├── read_with_filter.py │ ├── write_csv_array.py │ └── write_csv_dict.py ├── Ch2 │ ├── FinancialSample.xlsx │ ├── Inventory.csv │ ├── cell_styles.py │ ├── challenge_start.py │ ├── conditional_format.py │ ├── create_filters.py │ ├── create_workbook.py │ ├── explore_workbook.py │ └── workbook_content.py ├── Ch3 │ ├── Inventory.csv │ ├── challenge_start.py │ ├── xlsx_create.py │ ├── xlsx_format.py │ ├── xlsx_formula_conditional.py │ ├── xlsx_properties.py │ └── xlsx_tables.py └── Ch4 │ ├── FinancialSample.xlsx │ ├── Inventory.csv │ ├── challenge_start.py │ ├── pandas_explore.py │ ├── pandas_manipulate.py │ ├── pandas_read.py │ └── pandas_write.py └── requirements.txt /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.233.0/containers/python-3/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster 4 | ARG VARIANT="3.10" 5 | FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT} 6 | 7 | # [Choice] Node.js version: none, lts/*, 16, 14, 12, 10 8 | ARG NODE_VERSION="none" 9 | RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi 10 | 11 | # [Optional] If your pip requirements rarely change, uncomment this section to add them to the image. 12 | # COPY requirements.txt /tmp/pip-tmp/ 13 | # RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \ 14 | # && rm -rf /tmp/pip-tmp 15 | 16 | # [Optional] Uncomment this section to install additional OS packages. 17 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 18 | # && apt-get -y install --no-install-recommends 19 | 20 | # [Optional] Uncomment this line to install global node packages. 21 | # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 22 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python 3", 3 | "build": { 4 | "dockerfile": "Dockerfile", 5 | "context": "..", 6 | "args": { 7 | "VARIANT": "3.10", // Set Python version here 8 | "NODE_VERSION": "lts/*" 9 | } 10 | }, 11 | "settings": { 12 | "python.defaultInterpreterPath": "/usr/local/bin/python", 13 | "python.linting.enabled": true, 14 | "python.linting.pylintEnabled": true, 15 | "python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8", 16 | "python.formatting.blackPath": "/usr/local/py-utils/bin/black", 17 | "python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf", 18 | "python.linting.banditPath": "/usr/local/py-utils/bin/bandit", 19 | "python.linting.flake8Path": "/usr/local/py-utils/bin/flake8", 20 | "python.linting.mypyPath": "/usr/local/py-utils/bin/mypy", 21 | "python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle", 22 | "python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle", 23 | "python.linting.pylintPath": "/usr/local/py-utils/bin/pylint", 24 | "python.linting.pylintArgs": ["--disable=C0111"] 25 | }, 26 | "extensions": [ 27 | "ms-python.python", 28 | "ms-python.vscode-pylance" 29 | ], 30 | "remoteUser": "vscode", 31 | "onCreateCommand": "echo PS1='\"$ \"' >> ~/.bashrc", //Set Terminal Prompt to $ 32 | "postCreateCommand": "sh .devcontainer/startup.sh" 33 | } 34 | -------------------------------------------------------------------------------- /.devcontainer/startup.sh: -------------------------------------------------------------------------------- 1 | if [ -f requirements.txt ]; then 2 | pip install --user -r requirements.txt 3 | fi -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.bracketPairColorization.enabled": true, 3 | "editor.cursorBlinking": "solid", 4 | "editor.fontFamily": "ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace", 5 | "editor.fontLigatures": false, 6 | "editor.fontSize": 22, 7 | "editor.formatOnPaste": true, 8 | "editor.formatOnSave": true, 9 | "editor.lineNumbers": "on", 10 | "editor.matchBrackets": "always", 11 | "editor.minimap.enabled": false, 12 | "editor.smoothScrolling": true, 13 | "editor.tabSize": 2, 14 | "editor.useTabStops": true, 15 | "emmet.triggerExpansionOnTab": true, 16 | "explorer.openEditors.visible": 0, 17 | "files.autoSave": "afterDelay", 18 | "screencastMode.onlyKeyboardShortcuts": true, 19 | "terminal.integrated.fontSize": 18, 20 | "workbench.colorTheme": "Visual Studio Light", 21 | "workbench.fontAliasing": "antialiased", 22 | "workbench.statusBar.visible": true 23 | } 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /Finished/Ch1/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Finished/Ch1/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Solution for CSV Chapter challenge 4 | 5 | # import the csv module from the standard library 6 | import csv 7 | from decimal import Decimal 8 | 9 | 10 | def read_csv_to_array(filename): 11 | # define the array that will hold the data 12 | data = [] 13 | with open(filename, 'r') as csvfile: 14 | reader = csv.reader(csvfile) 15 | for row in reader: 16 | data.append(row) 17 | return data 18 | 19 | 20 | def write_array_to_csv(data, filename): 21 | with open(filename, 'w', newline='') as csvfile: 22 | writer = csv.writer(csvfile) 23 | writer.writerows(data) 24 | 25 | 26 | # Read the data into an array of arrays 27 | inventory_data = read_csv_to_array("Inventory.csv") 28 | 29 | # Add the new column to the headers 30 | headers = inventory_data[0] 31 | headers.append("Margin") 32 | 33 | # calculate margin for each row 34 | datarows = inventory_data[1:] 35 | for row in datarows: 36 | margin_value = Decimal(row[4]) - Decimal(row[3]) 37 | row.append(margin_value) 38 | 39 | # Write data to CSV file 40 | write_array_to_csv(inventory_data, "output.csv") 41 | -------------------------------------------------------------------------------- /Finished/Ch1/read_csv_array.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Reading CSV file into an array 4 | 5 | # import the csv module from the standard library 6 | import csv 7 | 8 | 9 | def read_csv_to_array(filename): 10 | # define the array that will hold the data 11 | data = [] 12 | with open(filename, 'r') as csvfile: 13 | reader = csv.reader(csvfile) 14 | for row in reader: 15 | data.append(row) 16 | return data 17 | 18 | 19 | # Read the data into an array of arrays 20 | inventory_data = read_csv_to_array("Inventory.csv") 21 | 22 | # Each row in the array is itself an array of values 23 | print(f"Items: {len(inventory_data)}") 24 | print(inventory_data[0]) # This will print the first row (header) 25 | print(inventory_data[1]) # This will print the first row of data 26 | # This will print the name of the item and quantity 27 | print(inventory_data[1][0], inventory_data[1][2]) 28 | -------------------------------------------------------------------------------- /Finished/Ch1/read_csv_dict.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Reading CSV file into an dictionary 4 | 5 | import csv 6 | import pprint 7 | 8 | 9 | def read_csv_to_dict(filename): 10 | data = {} 11 | with open(filename, 'r') as csvfile: 12 | reader = csv.DictReader(csvfile) 13 | for row in reader: 14 | # Add row data to dictionary with header as key 15 | data[row[reader.fieldnames[0]]] = row 16 | return data 17 | 18 | 19 | # Example usage 20 | inventory_data = read_csv_to_dict("Inventory.csv") 21 | 22 | # Accessing data 23 | pprint.pprint(inventory_data) 24 | # This will print the dictionary for the "Apple" row 25 | pprint.pprint(inventory_data["Apple"]) 26 | # This will print the price of Apple 27 | pprint.pprint(inventory_data["Apple"]["Consumer Price"]) 28 | -------------------------------------------------------------------------------- /Finished/Ch1/read_with_filter.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Reading CSV file into an array with a filter function 4 | 5 | import csv 6 | import pprint 7 | 8 | 9 | def read_csv_filter_rows(filename, filter_func): 10 | # array to hold the filtered data result 11 | filtered_data = [] 12 | 13 | with open(filename, 'r') as csvfile: 14 | reader = csv.reader(csvfile) 15 | for row in reader: 16 | if filter_func(row): 17 | filtered_data.append(row) 18 | return filtered_data 19 | 20 | # Example filter function (replace with your specific filtering criteria) 21 | 22 | 23 | def filter_by_category(row, category): 24 | return row[1] == category 25 | 26 | 27 | # Example usage (replace "fruits_and_vegetables.csv" with your filename) 28 | filtered_rows = read_csv_filter_rows( 29 | "Inventory.csv", lambda row: filter_by_category(row, "Fruits")) 30 | 31 | # Print filtered data 32 | pprint.pprint(filtered_rows) 33 | -------------------------------------------------------------------------------- /Finished/Ch1/write_csv_array.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Writing CSV file from an array 4 | 5 | import csv 6 | 7 | # Sample data 8 | data = [ 9 | ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"], 10 | ["Apple", "Fruits", 100, 0.50, 0.75], 11 | ["Banana", "Fruits", 150, 0.35, 0.50], 12 | ["Orange", "Fruits", 120, 0.45, 0.65], 13 | ["Grapes", "Fruits", 80, 0.60, 0.85], 14 | ["Strawberries", "Fruits", 90, 1.20, 1.50] 15 | ] 16 | 17 | # function to write the data 18 | 19 | 20 | def write_array_to_csv(data, filename): 21 | with open(filename, 'w', newline='') as csvfile: 22 | writer = csv.writer(csvfile) 23 | writer.writerows(data) 24 | 25 | 26 | # Write data to CSV file 27 | write_array_to_csv(data, "output.csv") 28 | -------------------------------------------------------------------------------- /Finished/Ch1/write_csv_dict.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Writing CSV file from an array 4 | 5 | import csv 6 | 7 | # define the column names that will be the header row 8 | fieldnames = ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"] 9 | 10 | # declare the sample data 11 | data = [ 12 | {"Item Name":"Apple", "Category":"Fruits", "Quantity":100, "Wholesale Price":0.50, "Consumer Price":0.75}, 13 | {"Item Name":"Banana", "Category":"Fruits", "Quantity":150, "Wholesale Price":0.35, "Consumer Price":0.50}, 14 | {"Item Name":"Orange", "Category":"Fruits", "Quantity":120, "Wholesale Price":0.45, "Consumer Price":0.65}, 15 | {"Item Name":"Grapes", "Category":"Fruits", "Quantity":80, "Wholesale Price":0.60, "Consumer Price":0.85}, 16 | {"Item Name":"Strawberries", "Category":"Fruits", "Quantity":90, "Wholesale Price":1.20, "Consumer Price":1.50} 17 | ] 18 | 19 | # function to write the data 20 | def write_dict_to_csv(data, filename): 21 | with open(filename, 'w', newline='') as csvfile: 22 | writer = csv.DictWriter(csvfile, fieldnames=fieldnames) 23 | # Write header row 24 | writer.writeheader() 25 | # Write data rows 26 | writer.writerows(data) 27 | 28 | # write the data to the file 29 | write_dict_to_csv(data, "output.csv") 30 | -------------------------------------------------------------------------------- /Finished/Ch2/FinancialSample.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-working-with-spreadsheet-and-excel-data-4554075/98b87f38e4728fef58842387541e41d56021f735/Finished/Ch2/FinancialSample.xlsx -------------------------------------------------------------------------------- /Finished/Ch2/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Finished/Ch2/cell_styles.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Manipulate cell content and styling 4 | 5 | from openpyxl import Workbook 6 | from openpyxl.styles import Font, Alignment, Border, Side 7 | import openpyxl.styles.numbers as opnumstyle 8 | import datetime 9 | 10 | 11 | # Create a new workbook 12 | wb = Workbook() 13 | 14 | # Get the active worksheet and name it "TestSheet" 15 | sheet = wb.active 16 | sheet.title = "First" 17 | 18 | # Add some data to the new sheet 19 | sheet["A1"] = "Test Data" 20 | sheet["B1"] = 123.4567 21 | sheet["C1"] = datetime.datetime(2030, 4, 1) 22 | 23 | # Inspect the default styles of each cell 24 | print(sheet["A1"].style) 25 | print(sheet["B1"].number_format) 26 | print(sheet["C1"].number_format) 27 | 28 | # Use some built-in styles 29 | # sheet["A1"].style = "Title" 30 | # sheet["B1"].number_format = opnumstyle.FORMAT_CURRENCY_USD_SIMPLE 31 | # sheet["B1"].style = "Calculation" 32 | # sheet["C1"].number_format= opnumstyle.FORMAT_DATE_DDMMYY 33 | # sheet["C1"].style = "Accent2" 34 | 35 | # Create styles using Fonts and Colors 36 | italic_font = Font(italic=True, size=16) 37 | colored_text = Font(name="Courier New", size=20, color="000000FF") 38 | centered_text = Alignment(horizontal="center", vertical="top") 39 | border_side = Side(border_style="mediumDashed") 40 | cell_border = Border(top=border_side, right=border_side, 41 | left=border_side, bottom=border_side) 42 | 43 | sheet["A1"].font = italic_font 44 | sheet["B1"].font = colored_text 45 | sheet["B1"].alignment = centered_text 46 | sheet["C1"].border = cell_border 47 | 48 | sheet.column_dimensions['A'].width = 30 49 | sheet.row_dimensions[1].height = 50 50 | 51 | # Save the workbook 52 | wb.save("StyledCells.xlsx") 53 | -------------------------------------------------------------------------------- /Finished/Ch2/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Split a single worksheet into multiple worksheets 4 | 5 | import openpyxl 6 | from openpyxl.utils.cell import column_index_from_string 7 | 8 | 9 | def split_workbook(workbook, source_sheet_name, split_column): 10 | source_worksheet = workbook[source_sheet_name] 11 | 12 | new_sheets = set() 13 | current_worksheet = None 14 | 15 | for row in source_worksheet.iter_rows(min_row=2): # Skip header row 16 | # Get value from the specified column 17 | col_indx = column_index_from_string(split_column) - 1 18 | value = row[col_indx].value 19 | 20 | if value not in new_sheets: 21 | new_sheets.add(value) 22 | 23 | # Create a new worksheet for the new value 24 | current_worksheet = workbook.create_sheet(title=value) 25 | else: 26 | current_worksheet = workbook[value] 27 | 28 | # Copy the row to the appropriate worksheet 29 | newrow = [] 30 | for cell in row: 31 | newrow.append(cell.value) 32 | 33 | current_worksheet.append(newrow) 34 | 35 | # When complete, return the list of sheets that were added 36 | return new_sheets 37 | 38 | 39 | # Example usage 40 | filename = "FinancialSample.xlsx" 41 | wb = openpyxl.load_workbook(filename) 42 | 43 | source_sheet_name = "SalesData" 44 | added_sheets = split_workbook(wb, source_sheet_name, "B") 45 | 46 | # Add the auto-filters to each worksheet 47 | for sheet_name in added_sheets: 48 | sheet = wb[sheet_name] 49 | filters = sheet.auto_filter 50 | filters.ref = sheet.dimensions 51 | 52 | # when the loop completes, save the new sheet 53 | wb.save("new"+filename) 54 | -------------------------------------------------------------------------------- /Finished/Ch2/conditional_format.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Apply conditional formatting to a worksheet 4 | 5 | import openpyxl 6 | from openpyxl.formatting import Rule 7 | from openpyxl.styles import Font, PatternFill 8 | from openpyxl.styles.differential import DifferentialStyle 9 | 10 | 11 | filename = "FinancialSample.xlsx" 12 | 13 | # Load the workbook 14 | workbook = openpyxl.load_workbook(filename) 15 | sheet = workbook["SalesData"] 16 | 17 | # define the style to represent the formatting 18 | red_color = "ffd2d2" 19 | bold_text = Font(bold=True, color="00FF0000") 20 | red_fill = PatternFill(bgColor=red_color, fill_type='solid') 21 | diff_style = DifferentialStyle(font=bold_text, fill=red_fill) 22 | 23 | # create a rule for the condition 24 | rule = Rule(type="expression", dxf=diff_style, formula=["$L1<10000"]) 25 | 26 | # add the rule to the entire sheet 27 | dimensions = sheet.dimensions 28 | sheet.conditional_formatting.add(dimensions, rule) 29 | 30 | workbook.save("CondFormat.xlsx") 31 | print("Workbook created successfully!") 32 | -------------------------------------------------------------------------------- /Finished/Ch2/create_filters.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Add column filters to a sheet 4 | 5 | import csv 6 | from openpyxl import Workbook 7 | 8 | 9 | def read_csv_to_array(filename): 10 | # define the array that will hold the data 11 | data = [] 12 | with open(filename, 'r') as csvfile: 13 | reader = csv.reader(csvfile) 14 | for row in reader: 15 | data.append(row) 16 | return data 17 | 18 | 19 | # Read the data into an array of arrays 20 | inventory_data = read_csv_to_array("Inventory.csv") 21 | 22 | # Create a new workbook 23 | wb = Workbook() 24 | 25 | # Get the active worksheet and name it "TestSheet" 26 | sheet = wb.active 27 | sheet.title = "Inventory" 28 | 29 | for row in inventory_data: 30 | sheet.append(row) 31 | 32 | # Add the filters to the columns 33 | filters = sheet.auto_filter 34 | filters.ref = sheet.dimensions 35 | 36 | wb.save("Inventory.xlsx") 37 | -------------------------------------------------------------------------------- /Finished/Ch2/create_workbook.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Create a new workbook with worksheets and add content 4 | 5 | from openpyxl import Workbook 6 | import datetime 7 | import random 8 | 9 | # Create a new workbook 10 | wb = Workbook() 11 | 12 | # Get the active worksheet and name it "TestSheet" 13 | sheet = wb.active 14 | sheet.title = "First" 15 | 16 | # Add some data to the new sheet 17 | sheet["A1"] = "Test Data" 18 | sheet["B1"] = 123.4567 19 | sheet["C1"] = datetime.datetime(2030, 4, 1) 20 | 21 | # Use the cell() function to fill a row with values 22 | for i in range(1, 11): 23 | sheet.cell(row = 5, column = i).value = random.randint(1, 50) 24 | 25 | # Create a second worksheet 26 | sheet2 = wb.create_sheet("Second") 27 | sheet2.cell(row=2, column=2).value = "More Data" 28 | 29 | # Use the append() function to add rows to the end of the sheet 30 | sheet2.append(["One","Two","Three"]) 31 | sheet2.append(["One","Two","Three"]) 32 | sheet2.append(["One","Two","Three"]) 33 | 34 | # Save the workbook - values don't update until we do this! 35 | wb.save("NewWorkbook.xlsx") 36 | 37 | print("Workbook created successfully!") 38 | -------------------------------------------------------------------------------- /Finished/Ch2/explore_workbook.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Open, load, and explore workbook content 4 | 5 | import openpyxl 6 | 7 | filename = "FinancialSample.xlsx" 8 | 9 | # Load the workbook 10 | workbook = openpyxl.load_workbook(filename) 11 | 12 | # Print basic information 13 | print(f"Number of worksheets: {len(workbook.sheetnames)}") 14 | 15 | # Explore each worksheet 16 | for worksheet_name in workbook.sheetnames: 17 | worksheet = workbook[worksheet_name] 18 | print(f"\nWorksheet: {worksheet_name}") 19 | 20 | # Get dimensions 21 | dimensions = worksheet.dimensions 22 | print(f" - Dimensions: {dimensions}") 23 | 24 | print(f"Min row: {worksheet.min_row}") 25 | print(f"Max row: {worksheet.max_row}") 26 | print(f"Min column: {worksheet.min_column}") 27 | print(f"Max column: {worksheet.max_column}") 28 | 29 | # Check if the worksheet is empty 30 | if worksheet.max_row == 1 and worksheet.max_column == 1: 31 | print(" - Worksheet is empty") 32 | else: 33 | # Get a cell (e.g., top-left corner) 34 | cell = worksheet["A1"] 35 | print(f" - Top-left cell value: {cell.value}") 36 | 37 | cell = worksheet.cell(row=worksheet.max_row,column=worksheet.max_column) 38 | print(f" - Bottom-Right cell value: {cell.value}") 39 | -------------------------------------------------------------------------------- /Finished/Ch2/workbook_content.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Manipulate workbook content 4 | 5 | import openpyxl 6 | from openpyxl.comments import Comment 7 | from collections import defaultdict 8 | 9 | 10 | # Create a new workbook 11 | filename = "FinancialSample.xlsx" 12 | 13 | # Load the workbook 14 | wb = openpyxl.load_workbook(filename) 15 | 16 | # Get the active worksheet 17 | sheet = wb.active 18 | 19 | # Get entire column or row of cells 20 | col = sheet["C"] 21 | row = sheet[10] 22 | print(f"{len(col)} cells in column") 23 | print(f"{len(row)} cells in row") 24 | 25 | # Get a range of cells 26 | range = sheet["A2:B7"] 27 | print(f"{len(range)} items in range") 28 | print(range) 29 | 30 | # iterate over rows and columns 31 | for col in sheet.iter_cols(min_row=2,max_row=3,min_col=2,max_col=5): 32 | for cell in col: 33 | print(cell.value) 34 | 35 | counter = defaultdict(int) 36 | for row in sheet.iter_rows(min_row=2,min_col=3,max_col=3): 37 | for cell in row: 38 | counter[cell.value] += 1 39 | print(counter) 40 | 41 | 42 | # create a cell with a comment in it 43 | cell = sheet["A1"] 44 | cell.comment = Comment("This is a comment", "Comment Author") 45 | 46 | # save the workbook 47 | wb.save("Content.xlsx") 48 | -------------------------------------------------------------------------------- /Finished/Ch3/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Finished/Ch3/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Read a single CSV file and split it into multiple worksheets 4 | 5 | import csv 6 | import xlsxwriter 7 | from collections import defaultdict 8 | 9 | 10 | # create a dictionary that will map strings to lists of data that 11 | # represent rows to go with that key name 12 | ws_dict = defaultdict(list) 13 | headers = [] 14 | 15 | filename = "Inventory.csv" 16 | 17 | def read_csv_to_array(filename): 18 | # define the array that will hold the data 19 | with open(filename, 'r') as csvfile: 20 | reader = csv.reader(csvfile) 21 | 22 | # read the headers, we will need this for each sheet 23 | global headers 24 | headers = next(reader) 25 | # read the data and distribute it to each key in the dictionary 26 | for row in reader: 27 | ws_dict[row[1]].append(row) 28 | 29 | # Read the data into the dictionary 30 | read_csv_to_array(filename) 31 | 32 | # create the workbook output 33 | workbook = xlsxwriter.Workbook("Inventory.xlsx") 34 | # create a worksheet for each key in the dictionary 35 | ws_names = ws_dict.keys() 36 | for name in ws_names: 37 | ws = workbook.add_worksheet(name) 38 | # write the header row to the sheet 39 | ws.write_row(0,0,headers) 40 | 41 | # now write all the data for that group to the current sheet 42 | datalist = ws_dict[name] 43 | for i, row in enumerate(datalist, start=1): 44 | ws.write_row(i, 0, row) 45 | 46 | # set the zoom and autofit the columns 47 | ws.set_zoom(200) 48 | ws.autofit() 49 | 50 | # save the workbook 51 | workbook.close() 52 | -------------------------------------------------------------------------------- /Finished/Ch3/xlsx_create.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter basic operations 4 | 5 | 6 | import xlsxwriter 7 | import datetime 8 | 9 | # create the workbook and add a worksheet 10 | workbook = xlsxwriter.Workbook("XlsxBasics.xlsx") 11 | worksheet = workbook.add_worksheet("Test Sheet") 12 | 13 | # Use Letter/Row notation 14 | worksheet.write("A1", "Hello world") 15 | # Use Row,Col notation 16 | worksheet.write(1, 0, "Hello world") 17 | 18 | # There are specific write() functions for different data types 19 | worksheet.write_number(2, 0, 12345) 20 | worksheet.write_boolean(3, 0, True) 21 | worksheet.write_url(4, 0, 'https://www.python.org/') 22 | 23 | # Write a datetime 24 | date_time = datetime.datetime.strptime('2030-07-28', '%Y-%m-%d') 25 | date_format = workbook.add_format({'num_format': 'd mmmm yyyy'}) 26 | worksheet.write_datetime(5, 0, date_time, date_format) 27 | 28 | # write multiple values into rows and columns 29 | values = ["Good", "Morning", "Excel"] 30 | worksheet.write_row("A6", values) 31 | worksheet.write_column("D1", values) 32 | 33 | # set the zoom on the sheet 34 | worksheet.set_zoom(200) 35 | 36 | # save the workbook 37 | workbook.close() 38 | -------------------------------------------------------------------------------- /Finished/Ch3/xlsx_format.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter formatting 4 | 5 | 6 | import xlsxwriter 7 | 8 | # Sample data 9 | data = [ 10 | ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"], 11 | ["Apple", "Fruits", 100, 0.50, 0.75], 12 | ["Banana", "Fruits", 150, 0.35, 0.50], 13 | ["Orange", "Fruits", 120, 0.45, 0.65], 14 | ["Grapes", "Fruits", 80, 0.60, 0.85], 15 | ["Strawberries", "Fruits", 90, 1.20, 1.50] 16 | ] 17 | 18 | # create the workbook 19 | workbook = xlsxwriter.Workbook('Inventory.xlsx') 20 | worksheet = workbook.add_worksheet("Inventory") 21 | 22 | # Use the add_format function to define formats that you can use later 23 | # in the worksheet. NOTE: If you change the format then ALL prior instances 24 | # of the format will be saved as the most recent one 25 | fmt_bold = workbook.add_format({'bold': True}) 26 | fmt_money = workbook.add_format( 27 | {'font_color': 'green', 'num_format': '$#,##0.00'}) 28 | 29 | # write the data into the workbook 30 | worksheet.write_row(0, 0, data[0], fmt_bold) 31 | for row, itemlist in enumerate(data[1:], start=1): 32 | # worksheet.write_row(row, 0, itemlist) 33 | worksheet.write(row, 0, itemlist[0]) 34 | worksheet.write(row, 1, itemlist[1], fmt_bold) 35 | worksheet.write(row, 2, itemlist[2]) 36 | worksheet.write(row, 3, itemlist[3], fmt_money) 37 | worksheet.write(row, 4, itemlist[4], fmt_money) 38 | 39 | 40 | worksheet.set_zoom(200) 41 | worksheet.autofit() 42 | 43 | workbook.close() 44 | -------------------------------------------------------------------------------- /Finished/Ch3/xlsx_formula_conditional.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter formulas and conditional formatting 4 | 5 | import csv 6 | import xlsxwriter 7 | 8 | 9 | def read_csv_to_array(filename): 10 | # define the array that will hold the data 11 | data = [] 12 | with open(filename, 'r') as csvfile: 13 | reader = csv.reader(csvfile) 14 | for row in reader: 15 | data.append(row) 16 | return data 17 | 18 | 19 | # Read the data into an array of arrays 20 | inventory_data = read_csv_to_array("Inventory.csv") 21 | 22 | # create the workbook 23 | workbook = xlsxwriter.Workbook('Conditional.xlsx') 24 | worksheet = workbook.add_worksheet("Inventory") 25 | 26 | fmt_bold = workbook.add_format({'bold': True}) 27 | fmt_money = workbook.add_format( 28 | {'font_color': 'green', 'num_format': '$#,##0.00'}) 29 | # define the format for the conditional expression 30 | fmt_cond = workbook.add_format({"bg_color": "#AAFFAA", "bold": True}) 31 | 32 | # write the data into the workbook 33 | worksheet.write_row(0, 0, inventory_data[0], fmt_bold) 34 | # add the new header for the margin 35 | worksheet.write(0, 5, "Margin", fmt_bold) 36 | # add the data to the worksheet 37 | for row, itemlist in enumerate(inventory_data[1:], start=1): 38 | # worksheet.write_row(row, 0, itemlist) 39 | worksheet.write(row, 0, itemlist[0]) 40 | worksheet.write(row, 1, itemlist[1], fmt_bold) 41 | worksheet.write_number(row, 2, int(itemlist[2])) 42 | worksheet.write_number(row, 3, float(itemlist[3]), fmt_money) 43 | worksheet.write_number(row, 4, float(itemlist[4]), fmt_money) 44 | # calculate the row and column for the formula 45 | worksheet.write_formula(row, 5, f"=E{row+1}-D{row+1}", fmt_money) 46 | 47 | # add the conditional formatting 48 | # worksheet.conditional_format(1, 5, len(inventory_data), 5, { 49 | # "type": "cell", 50 | # "criteria": ">=", 51 | # "value": 0.75, 52 | # "format": fmt_cond 53 | # }) 54 | worksheet.conditional_format(1, 0, len(inventory_data), 5, 55 | {'type': 'formula', 56 | 'criteria': '=$F2>=.75', 57 | 'format': fmt_cond 58 | }) 59 | 60 | worksheet.set_zoom(150) 61 | worksheet.autofit() 62 | 63 | workbook.close() 64 | -------------------------------------------------------------------------------- /Finished/Ch3/xlsx_properties.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter document properties 4 | 5 | import xlsxwriter 6 | 7 | workbook = xlsxwriter.Workbook("Properties.xlsx") 8 | worksheet = workbook.add_worksheet() 9 | 10 | props = { 11 | "title": "Document Properties Example", 12 | "subject": "Shows how to use document properties in XlsxWriter", 13 | "author": "Joe Marini", 14 | "company": "LinkedIn Learning", 15 | "manager": "Dr. Heinz Doofenshmirtz", 16 | "category": "Example spreadsheets", 17 | "keywords": "Properties, Sample, XlsxWriter", 18 | "comments": "Created using XlsxWriter as a LinkedIn Learning Example", 19 | } 20 | 21 | # set the standard properties 22 | workbook.set_properties(props) 23 | 24 | # set some custom properties 25 | workbook.set_custom_property("Checked by", "Perry P") 26 | workbook.set_custom_property("Approved", True) 27 | 28 | workbook.close() 29 | -------------------------------------------------------------------------------- /Finished/Ch3/xlsx_tables.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter Excel Tables 4 | 5 | 6 | import xlsxwriter 7 | 8 | # Sample data 9 | data = [ 10 | ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"], 11 | ["Apple", "Fruits", 100, 0.50, 0.75], 12 | ["Banana", "Fruits", 150, 0.35, 0.50], 13 | ["Orange", "Fruits", 120, 0.45, 0.65], 14 | ["Grapes", "Fruits", 80, 0.60, 0.85], 15 | ["Strawberries", "Fruits", 90, 1.20, 1.50] 16 | ] 17 | 18 | # create the workbook 19 | workbook = xlsxwriter.Workbook('Tables.xlsx') 20 | worksheet = workbook.add_worksheet("Inventory") 21 | 22 | fmt_bold = workbook.add_format({'bold': True}) 23 | fmt_money = workbook.add_format( 24 | {'font_color': 'green', 'num_format': '$#,##0.00'}) 25 | 26 | # write the data into the workbook 27 | worksheet.write_row(0, 0, data[0], fmt_bold) 28 | for row, itemlist in enumerate(data[1:], start=1): 29 | # worksheet.write_row(row, 0, itemlist) 30 | worksheet.write(row, 0, itemlist[0]) 31 | worksheet.write(row, 1, itemlist[1], fmt_bold) 32 | worksheet.write(row, 2, itemlist[2]) 33 | worksheet.write(row, 3, itemlist[3], fmt_money) 34 | worksheet.write(row, 4, itemlist[4], fmt_money) 35 | 36 | # define a table for the worksheet 37 | table_options = { 38 | "name": "InventoryData", 39 | "autofilter": True, 40 | "banded_rows": False, 41 | "first_column": True, 42 | "columns": [ 43 | {"header": data[0][0]}, 44 | {"header": data[0][1]}, 45 | {"header": data[0][2]}, 46 | {"header": data[0][3]}, 47 | {"header": data[0][4]}, 48 | ] 49 | } 50 | worksheet.add_table("A1:E6", table_options) 51 | 52 | worksheet.set_zoom(200) 53 | worksheet.autofit() 54 | 55 | workbook.close() 56 | -------------------------------------------------------------------------------- /Finished/Ch4/FinancialSample.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-working-with-spreadsheet-and-excel-data-4554075/98b87f38e4728fef58842387541e41d56021f735/Finished/Ch4/FinancialSample.xlsx -------------------------------------------------------------------------------- /Finished/Ch4/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Finished/Ch4/challenge_solution.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Challenge solution for working with Pandas data 4 | 5 | 6 | import pandas as pd 7 | 8 | 9 | # read the original data set 10 | df_sales = pd.read_excel("FinancialSample.xlsx") 11 | 12 | headers = ["Product", "Gross Sales", "Profits"] 13 | # calculate the summary data 14 | summary_data = [] 15 | for prod_name in df_sales["Product"].unique(): 16 | sales_total = df_sales.loc[df_sales['Product'] 17 | == prod_name, "Gross Sales"].sum() 18 | profit_total = df_sales.loc[df_sales['Product'] 19 | == prod_name, "Profit"].sum() 20 | summary_data.append([prod_name, sales_total, profit_total]) 21 | 22 | # create a new DataFrame with the summary data 23 | df_summary = pd.DataFrame(summary_data, columns=headers) 24 | 25 | # write the data to the new sheet 26 | with pd.ExcelWriter("FinancialSample.xlsx", engine="openpyxl", mode='a') as xlw: 27 | df_summary.to_excel(xlw, sheet_name="Summary", index=False) 28 | -------------------------------------------------------------------------------- /Finished/Ch4/pandas_explore.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Exploring DataFrame data and structure 4 | 5 | import pandas as pd 6 | import locale 7 | 8 | 9 | df = pd.read_excel("FinancialSample.xlsx") 10 | 11 | # Set the display options for Pandas 12 | pd.set_option("display.max.columns", None) 13 | pd.set_option("display.precision", 2) 14 | 15 | # Examine the structure of the data set 16 | print(df.shape) 17 | print(df.describe()) 18 | 19 | # View a subset of the data 20 | print(df.head(5)) 21 | print(df.tail(5)) 22 | 23 | # Get the number of values for a given column 24 | print(df["Product"].value_counts()) 25 | 26 | # Get the unique data values for a column 27 | print(df["Product"].unique()) 28 | 29 | # Get the min and max values of a column 30 | print(df["Profit"].max()) 31 | print(df["Profit"].min()) 32 | 33 | # use the loc() function to conditionally sum a column 34 | print(df["Profit"].sum()) 35 | print(df.loc[df['Product'] == "Carretera", "Profit"].sum()) 36 | 37 | # Get the sum of the Profit column for each of the Product types 38 | # and format the output as currency in the user's locale 39 | locale.setlocale(locale.LC_ALL, '') 40 | for prod_name in df["Product"].unique(): 41 | total = df.loc[df['Product'] == prod_name, "Profit"].sum() 42 | print(f"Profits for {prod_name}:", locale.currency(total, grouping=True)) 43 | -------------------------------------------------------------------------------- /Finished/Ch4/pandas_manipulate.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Manipulating DataFrame content 4 | 5 | import pandas as pd 6 | 7 | 8 | df = pd.read_csv("Inventory.csv") 9 | print(df.shape) 10 | 11 | # Create a new column of data 12 | df["Margin"] = df["Consumer Price"] - df["Wholesale Price"] 13 | print(df.shape) 14 | print(df.head()) 15 | 16 | # Modify a column in-place 17 | df["Category"] = df["Category"].apply(lambda x: x.upper()) 18 | print(df) 19 | 20 | # rename a column in-place 21 | df.rename(columns={ 22 | "Wholesale Price":"Wholesale", 23 | "Consumer Price":"Consumer" 24 | }, inplace=True) 25 | print(df.head()) 26 | 27 | # Drop a column 28 | df.drop("Margin", inplace=True, axis=1) 29 | print(df.head()) 30 | -------------------------------------------------------------------------------- /Finished/Ch4/pandas_read.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Using Pandas library to read CSV and Excel data 4 | 5 | import pandas as pd 6 | 7 | 8 | # read a CSV file using read_csv 9 | df = pd.read_csv("Inventory.csv") 10 | print(df) 11 | 12 | # read just a portion of the file 13 | df = pd.read_csv("Inventory.csv", skiprows=lambda x: x >= 1 and x < 15, nrows=15) 14 | print(df) 15 | 16 | # read an Excel file 17 | df = pd.read_excel("FinancialSample.xlsx", usecols="A:E,H", nrows=15) 18 | print(df) 19 | print(df.dtypes) 20 | 21 | # Get information about a workbook 22 | file = pd.ExcelFile('FinancialSample.xlsx') 23 | print("Sheet names are:", file.sheet_names) 24 | -------------------------------------------------------------------------------- /Finished/Ch4/pandas_write.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Using Pandas library to write CSV and Excel data 4 | 5 | import pandas as pd 6 | 7 | # create a CSV file with Pandas 8 | data = [ 9 | {"Item Name":"Apple", "Category":"Fruits", "Quantity":100, "Wholesale Price":0.50, "Consumer Price":0.75}, 10 | {"Item Name":"Banana", "Category":"Fruits", "Quantity":150, "Wholesale Price":0.35, "Consumer Price":0.50}, 11 | {"Item Name":"Orange", "Category":"Fruits", "Quantity":120, "Wholesale Price":0.45, "Consumer Price":0.65}, 12 | {"Item Name":"Grapes", "Category":"Fruits", "Quantity":80, "Wholesale Price":0.60, "Consumer Price":0.85}, 13 | {"Item Name":"Strawberries", "Category":"Fruits", "Quantity":90, "Wholesale Price":1.20, "Consumer Price":1.50} 14 | ] 15 | df = pd.DataFrame(data) 16 | df.to_csv("output.csv", index=False) 17 | 18 | # Convert a CSV file to an Excel file 19 | df = pd.read_csv("Inventory.csv") 20 | df.to_excel("Inventory.xlsx", sheet_name="Inventory", index=False) 21 | 22 | # append data to an existing workbook without overwriting 23 | with pd.ExcelWriter("Inventory.xlsx", engine="openpyxl", mode='a') as xlw: 24 | df.to_excel(xlw, sheet_name="Inventory Index", index_label="Index") 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2024 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | ATTRIBUTIONS: 8 | 9 | requests 10 | https://github.com/psf/requests/ 11 | License: Apache 2.0 12 | http://www.apache.org/licenses/ 13 | 14 | Please note, this project may automatically load third party code from external 15 | repositories (for example, NPM modules, Composer packages, or other dependencies). 16 | If so, such third party code may be subject to other license terms than as set 17 | forth above. In addition, such third party code may also depend on and load 18 | multiple tiers of dependencies. Please review the applicable licenses of the 19 | additional dependencies. 20 | 21 | 22 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 23 | 24 | " Apache License 25 | Version 2.0, January 2004 26 | http://www.apache.org/licenses/ 27 | 28 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 29 | 30 | 1. Definitions. 31 | 32 | ""License"" shall mean the terms and conditions for use, reproduction, 33 | and distribution as defined by Sections 1 through 9 of this document. 34 | 35 | ""Licensor"" shall mean the copyright owner or entity authorized by 36 | the copyright owner that is granting the License. 37 | 38 | ""Legal Entity"" shall mean the union of the acting entity and all 39 | other entities that control, are controlled by, or are under common 40 | control with that entity. For the purposes of this definition, 41 | ""control"" means (i) the power, direct or indirect, to cause the 42 | direction or management of such entity, whether by contract or 43 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 44 | outstanding shares, or (iii) beneficial ownership of such entity. 45 | 46 | ""You"" (or ""Your"") shall mean an individual or Legal Entity 47 | exercising permissions granted by this License. 48 | 49 | ""Source"" form shall mean the preferred form for making modifications, 50 | including but not limited to software source code, documentation 51 | source, and configuration files. 52 | 53 | ""Object"" form shall mean any form resulting from mechanical 54 | transformation or translation of a Source form, including but 55 | not limited to compiled object code, generated documentation, 56 | and conversions to other media types. 57 | 58 | ""Work"" shall mean the work of authorship, whether in Source or 59 | Object form, made available under the License, as indicated by a 60 | copyright notice that is included in or attached to the work 61 | (an example is provided in the Appendix below). 62 | 63 | ""Derivative Works"" shall mean any work, whether in Source or Object 64 | form, that is based on (or derived from) the Work and for which the 65 | editorial revisions, annotations, elaborations, or other modifications 66 | represent, as a whole, an original work of authorship. For the purposes 67 | of this License, Derivative Works shall not include works that remain 68 | separable from, or merely link (or bind by name) to the interfaces of, 69 | the Work and Derivative Works thereof. 70 | 71 | ""Contribution"" shall mean any work of authorship, including 72 | the original version of the Work and any modifications or additions 73 | to that Work or Derivative Works thereof, that is intentionally 74 | submitted to Licensor for inclusion in the Work by the copyright owner 75 | or by an individual or Legal Entity authorized to submit on behalf of 76 | the copyright owner. For the purposes of this definition, ""submitted"" 77 | means any form of electronic, verbal, or written communication sent 78 | to the Licensor or its representatives, including but not limited to 79 | communication on electronic mailing lists, source code control systems, 80 | and issue tracking systems that are managed by, or on behalf of, the 81 | Licensor for the purpose of discussing and improving the Work, but 82 | excluding communication that is conspicuously marked or otherwise 83 | designated in writing by the copyright owner as ""Not a Contribution."" 84 | 85 | ""Contributor"" shall mean Licensor and any individual or Legal Entity 86 | on behalf of whom a Contribution has been received by Licensor and 87 | subsequently incorporated within the Work. 88 | 89 | 2. Grant of Copyright License. Subject to the terms and conditions of 90 | this License, each Contributor hereby grants to You a perpetual, 91 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 92 | copyright license to reproduce, prepare Derivative Works of, 93 | publicly display, publicly perform, sublicense, and distribute the 94 | Work and such Derivative Works in Source or Object form. 95 | 96 | 3. Grant of Patent License. Subject to the terms and conditions of 97 | this License, each Contributor hereby grants to You a perpetual, 98 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 99 | (except as stated in this section) patent license to make, have made, 100 | use, offer to sell, sell, import, and otherwise transfer the Work, 101 | where such license applies only to those patent claims licensable 102 | by such Contributor that are necessarily infringed by their 103 | Contribution(s) alone or by combination of their Contribution(s) 104 | with the Work to which such Contribution(s) was submitted. If You 105 | institute patent litigation against any entity (including a 106 | cross-claim or counterclaim in a lawsuit) alleging that the Work 107 | or a Contribution incorporated within the Work constitutes direct 108 | or contributory patent infringement, then any patent licenses 109 | granted to You under this License for that Work shall terminate 110 | as of the date such litigation is filed. 111 | 112 | 4. Redistribution. You may reproduce and distribute copies of the 113 | Work or Derivative Works thereof in any medium, with or without 114 | modifications, and in Source or Object form, provided that You 115 | meet the following conditions: 116 | 117 | (a) You must give any other recipients of the Work or 118 | Derivative Works a copy of this License; and 119 | 120 | (b) You must cause any modified files to carry prominent notices 121 | stating that You changed the files; and 122 | 123 | (c) You must retain, in the Source form of any Derivative Works 124 | that You distribute, all copyright, patent, trademark, and 125 | attribution notices from the Source form of the Work, 126 | excluding those notices that do not pertain to any part of 127 | the Derivative Works; and 128 | 129 | (d) If the Work includes a ""NOTICE"" text file as part of its 130 | distribution, then any Derivative Works that You distribute must 131 | include a readable copy of the attribution notices contained 132 | within such NOTICE file, excluding those notices that do not 133 | pertain to any part of the Derivative Works, in at least one 134 | of the following places: within a NOTICE text file distributed 135 | as part of the Derivative Works; within the Source form or 136 | documentation, if provided along with the Derivative Works; or, 137 | within a display generated by the Derivative Works, if and 138 | wherever such third-party notices normally appear. The contents 139 | of the NOTICE file are for informational purposes only and 140 | do not modify the License. You may add Your own attribution 141 | notices within Derivative Works that You distribute, alongside 142 | or as an addendum to the NOTICE text from the Work, provided 143 | that such additional attribution notices cannot be construed 144 | as modifying the License. 145 | 146 | You may add Your own copyright statement to Your modifications and 147 | may provide additional or different license terms and conditions 148 | for use, reproduction, or distribution of Your modifications, or 149 | for any such Derivative Works as a whole, provided Your use, 150 | reproduction, and distribution of the Work otherwise complies with 151 | the conditions stated in this License. 152 | 153 | 5. Submission of Contributions. Unless You explicitly state otherwise, 154 | any Contribution intentionally submitted for inclusion in the Work 155 | by You to the Licensor shall be under the terms and conditions of 156 | this License, without any additional terms or conditions. 157 | Notwithstanding the above, nothing herein shall supersede or modify 158 | the terms of any separate license agreement you may have executed 159 | with Licensor regarding such Contributions. 160 | 161 | 6. Trademarks. This License does not grant permission to use the trade 162 | names, trademarks, service marks, or product names of the Licensor, 163 | except as required for reasonable and customary use in describing the 164 | origin of the Work and reproducing the content of the NOTICE file. 165 | 166 | 7. Disclaimer of Warranty. Unless required by applicable law or 167 | agreed to in writing, Licensor provides the Work (and each 168 | Contributor provides its Contributions) on an ""AS IS"" BASIS, 169 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 170 | implied, including, without limitation, any warranties or conditions 171 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 172 | PARTICULAR PURPOSE. You are solely responsible for determining the 173 | appropriateness of using or redistributing the Work and assume any 174 | risks associated with Your exercise of permissions under this License. 175 | 176 | 8. Limitation of Liability. In no event and under no legal theory, 177 | whether in tort (including negligence), contract, or otherwise, 178 | unless required by applicable law (such as deliberate and grossly 179 | negligent acts) or agreed to in writing, shall any Contributor be 180 | liable to You for damages, including any direct, indirect, special, 181 | incidental, or consequential damages of any character arising as a 182 | result of this License or out of the use or inability to use the 183 | Work (including but not limited to damages for loss of goodwill, 184 | work stoppage, computer failure or malfunction, or any and all 185 | other commercial damages or losses), even if such Contributor 186 | has been advised of the possibility of such damages. 187 | 188 | 9. Accepting Warranty or Additional Liability. While redistributing 189 | the Work or Derivative Works thereof, You may choose to offer, 190 | and charge a fee for, acceptance of support, warranty, indemnity, 191 | or other liability obligations and/or rights consistent with this 192 | License. However, in accepting such obligations, You may act only 193 | on Your own behalf and on Your sole responsibility, not on behalf 194 | of any other Contributor, and only if You agree to indemnify, 195 | defend, and hold each Contributor harmless for any liability 196 | incurred by, or claims asserted against, such Contributor by reason 197 | of your accepting any such warranty or additional liability." 198 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Python: Hands-On Working with Spreadsheet and Excel Data 2 | 3 | This is the repository for the LinkedIn Learning course Python: Working with Spreadsheet and Excel Data. The full course is available from [LinkedIn Learning][lil-course-url]. 4 | 5 | ![lil-thumbnail-url] 6 | 7 | In this hands-on course, technology industry veteran Joe Marini guides you through a variety of practical ways that you can leverage Python to work more effectively with data from spreadsheets and Excel. Find out how to read and write CSV data, as well as how to convert data to and from CSV. Learn how Python can help you in creating, reading, and writing Excel workbooks. Plus, explore ways you can use Python when you’re working with Excel sheet data and formulas. 8 | 9 | Spreadsheets are one of the most common tools you'll find in businesses today. They are used for everything from monitoring project status, to reporting financial information, to tracking inventory levels. Using the power of Python, you can create spreadsheets, extract data, and automate just about any workflow you can think of. By the time you finish this course, you'll be able to build all kinds of sophisticated automations for your data workflows. 10 | 11 | ## Installing 12 | 13 | 1. To use these exercise files, you must have the following installed: 14 | - Microsoft Excel (to view the results) 15 | - Python (at least 3.8) 16 | 2. Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree. 17 | 3. Install the pandas, openpyxl, and xlsxwriter libraries using pip 18 | - `pip install pandas, openpyxl, xlsxwriter` 19 | 20 | ### Instructor 21 | 22 | Joe Marini 23 | 24 | Technology Industry Veteran 25 | 26 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/joe-marini?u=104). 27 | 28 | 29 | 30 | [0]: # (Replace these placeholder URLs with actual course URLs) 31 | 32 | [lil-course-url]: https://www.linkedin.com/learning/advanced-hands-on-python-working-with-excel-and-spreadsheet-data 33 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/D560DAQHwOcX3aO5fcw/learning-public-crop_675_1200/0/1718642433673?e=2147483647&v=beta&t=gQcP_1yje1f5zrGj-tuSJXEwYjwbS-A_sT8cd2AReSw 34 | -------------------------------------------------------------------------------- /Start/Ch1/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Start/Ch1/read_csv_array.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Reading CSV file into an array 4 | 5 | # import the csv module from the standard library 6 | import csv 7 | 8 | def read_csv_to_array(filename): 9 | # define the array that will hold the data 10 | data = [] 11 | 12 | # Read the data into an array of arrays 13 | inventory_data = read_csv_to_array("Inventory.csv") 14 | 15 | # Each row in the array is itself an array of values 16 | 17 | -------------------------------------------------------------------------------- /Start/Ch1/read_csv_dict.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Reading CSV file into an dictionary 4 | 5 | import csv 6 | import pprint 7 | 8 | def read_csv_to_dict(filename): 9 | data = {} 10 | 11 | return data 12 | 13 | # Example usage 14 | inventory_data = read_csv_to_dict("Inventory.csv") 15 | 16 | # Accessing data 17 | -------------------------------------------------------------------------------- /Start/Ch1/read_with_filter.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Reading CSV file into an array with a filter function 4 | 5 | import csv 6 | import pprint 7 | 8 | def read_csv_filter_rows(filename): 9 | # array to hold the filtered data result 10 | filtered_data = [] 11 | 12 | with open(filename, 'r') as csvfile: 13 | reader = csv.reader(csvfile) 14 | for row in reader: 15 | filtered_data.append(row) 16 | return filtered_data 17 | 18 | # Filter function (replace with your specific filtering criteria) 19 | 20 | 21 | # Call the read function with a filter function 22 | filtered_rows = read_csv_filter_rows("Inventory.csv") 23 | 24 | # Print filtered data 25 | pprint.pprint(filtered_rows) 26 | -------------------------------------------------------------------------------- /Start/Ch1/write_csv_array.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Writing CSV file from an array 4 | 5 | import csv 6 | 7 | # Sample data 8 | data = [ 9 | ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"], 10 | ["Apple","Fruits",100,0.50,0.75], 11 | ["Banana","Fruits",150,0.35,0.50], 12 | ["Orange","Fruits",120,0.45,0.65], 13 | ["Grapes","Fruits",80,0.60,0.85], 14 | ["Strawberries","Fruits",90,1.20,1.50] 15 | ] 16 | 17 | 18 | # Write data to CSV file 19 | -------------------------------------------------------------------------------- /Start/Ch1/write_csv_dict.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Writing CSV file from an array 4 | 5 | import csv 6 | 7 | # define the column names that will be the header row 8 | 9 | 10 | # declare the sample data 11 | data = [ 12 | {"Item Name":"Apple", "Category":"Fruits", "Quantity":100, "Wholesale Price":0.50, "Consumer Price":0.75}, 13 | {"Item Name":"Banana", "Category":"Fruits", "Quantity":150, "Wholesale Price":0.35, "Consumer Price":0.50}, 14 | {"Item Name":"Orange", "Category":"Fruits", "Quantity":120, "Wholesale Price":0.45, "Consumer Price":0.65}, 15 | {"Item Name":"Grapes", "Category":"Fruits", "Quantity":80, "Wholesale Price":0.60, "Consumer Price":0.85}, 16 | {"Item Name":"Strawberries", "Category":"Fruits", "Quantity":90, "Wholesale Price":1.20, "Consumer Price":1.50} 17 | ] 18 | 19 | # function to write the data 20 | 21 | 22 | # write the data to the file 23 | -------------------------------------------------------------------------------- /Start/Ch2/FinancialSample.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-working-with-spreadsheet-and-excel-data-4554075/98b87f38e4728fef58842387541e41d56021f735/Start/Ch2/FinancialSample.xlsx -------------------------------------------------------------------------------- /Start/Ch2/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Start/Ch2/cell_styles.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Manipulate cell content and styling 4 | 5 | from openpyxl import Workbook 6 | from openpyxl.styles import Font, Alignment, Border, Side 7 | import openpyxl.styles.numbers as opnumstyle 8 | import datetime 9 | 10 | 11 | # Create a new workbook 12 | wb = Workbook() 13 | 14 | # Get the active worksheet and name it "TestSheet" 15 | sheet = wb.active 16 | sheet.title = "First" 17 | 18 | # Add some data to the new sheet 19 | sheet["A1"] = "Test Data" 20 | sheet["B1"] = 123.4567 21 | sheet["C1"] = datetime.datetime(2030, 4, 1) 22 | 23 | # Inspect the default styles of each cell 24 | print(sheet["A1"].style) 25 | print(sheet["B1"].number_format) 26 | print(sheet["C1"].number_format) 27 | 28 | # Use some built-in styles 29 | 30 | 31 | # Create styles using Fonts and Colors 32 | 33 | 34 | # Save the workbook 35 | wb.save("StyledCells.xlsx") 36 | -------------------------------------------------------------------------------- /Start/Ch2/challenge_start.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Split a single worksheet into multiple worksheets 4 | 5 | import openpyxl 6 | -------------------------------------------------------------------------------- /Start/Ch2/conditional_format.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Apply conditional formatting to a worksheet 4 | 5 | import openpyxl 6 | from openpyxl.formatting import Rule 7 | from openpyxl.styles import Font, PatternFill 8 | from openpyxl.styles.differential import DifferentialStyle 9 | 10 | 11 | filename = "FinancialSample.xlsx" 12 | 13 | # Load the workbook 14 | workbook = openpyxl.load_workbook(filename) 15 | sheet = workbook["SalesData"] 16 | 17 | # define the style to represent the formatting 18 | 19 | 20 | # create a rule for the condition 21 | 22 | 23 | # add the rule to the entire sheet 24 | 25 | 26 | workbook.save("CondFormat.xlsx") 27 | print("Workbook created successfully!") 28 | -------------------------------------------------------------------------------- /Start/Ch2/create_filters.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Add column filters to a sheet 4 | 5 | import csv 6 | from openpyxl import Workbook 7 | 8 | 9 | def read_csv_to_array(filename): 10 | # define the array that will hold the data 11 | data = [] 12 | with open(filename, 'r') as csvfile: 13 | reader = csv.reader(csvfile) 14 | for row in reader: 15 | data.append(row) 16 | return data 17 | 18 | 19 | # Read the data into an array of arrays 20 | inventory_data = read_csv_to_array("Inventory.csv") 21 | 22 | # Create a new workbook 23 | wb = Workbook() 24 | 25 | # Get the active worksheet and name it "TestSheet" 26 | sheet = wb.active 27 | sheet.title = "Inventory" 28 | 29 | for row in inventory_data: 30 | sheet.append(row) 31 | 32 | # Add the filters to the columns 33 | 34 | 35 | wb.save("Inventory.xlsx") 36 | -------------------------------------------------------------------------------- /Start/Ch2/create_workbook.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Create a new workbook with worksheets and add content 4 | 5 | from openpyxl import Workbook 6 | import datetime 7 | import random 8 | 9 | # Create a new workbook 10 | 11 | 12 | # Get the active worksheet and name it "First" 13 | 14 | 15 | # Add some data to the new sheet 16 | 17 | 18 | # Use the cell() function to fill a row with values 19 | 20 | 21 | # Create a second worksheet 22 | 23 | 24 | # Use the append() function to add rows to the end of the sheet 25 | 26 | 27 | # Save the workbook - values don't update until we do this! 28 | -------------------------------------------------------------------------------- /Start/Ch2/explore_workbook.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Open, load, and explore workbook content 4 | 5 | import openpyxl 6 | 7 | filename = "FinancialSample.xlsx" 8 | 9 | # Load the workbook 10 | 11 | 12 | # Print basic information 13 | 14 | 15 | # Explore each worksheet 16 | 17 | 18 | # Get dimensions 19 | 20 | # Check if the worksheet is empty 21 | -------------------------------------------------------------------------------- /Start/Ch2/workbook_content.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Manipulate workbook content 4 | 5 | import openpyxl 6 | from openpyxl.comments import Comment 7 | from collections import defaultdict 8 | 9 | 10 | # Create a new workbook 11 | filename = "FinancialSample.xlsx" 12 | 13 | # Load the workbook 14 | wb = openpyxl.load_workbook(filename) 15 | 16 | # Get the active worksheet 17 | sheet = wb.active 18 | 19 | # Get entire column or row of cells 20 | 21 | 22 | # Get a range of cells 23 | 24 | 25 | # iterate over rows and columns 26 | 27 | 28 | # create a cell with a comment in it 29 | 30 | 31 | # save the workbook 32 | -------------------------------------------------------------------------------- /Start/Ch3/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Start/Ch3/challenge_start.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Read a single CSV file and split it into multiple worksheets 4 | 5 | import csv 6 | import xlsxwriter 7 | 8 | 9 | filename = "Inventory.csv" 10 | 11 | # PUT YOUR CHALLENGE CODE HERE 12 | -------------------------------------------------------------------------------- /Start/Ch3/xlsx_create.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter basic operations 4 | 5 | 6 | import xlsxwriter 7 | import datetime 8 | 9 | # create the workbook and add a worksheet 10 | 11 | 12 | # Use Letter/Row notation 13 | 14 | # Use Row,Col notation 15 | 16 | 17 | # There are specific write() functions for different data types 18 | 19 | 20 | # Write a datetime 21 | 22 | 23 | # write multiple values into rows and columns 24 | 25 | 26 | # set the zoom on the sheet 27 | 28 | 29 | # save the workbook 30 | -------------------------------------------------------------------------------- /Start/Ch3/xlsx_format.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter formatting 4 | 5 | 6 | import xlsxwriter 7 | 8 | # Sample data 9 | data = [ 10 | ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"], 11 | ["Apple", "Fruits", 100, 0.50, 0.75], 12 | ["Banana", "Fruits", 150, 0.35, 0.50], 13 | ["Orange", "Fruits", 120, 0.45, 0.65], 14 | ["Grapes", "Fruits", 80, 0.60, 0.85], 15 | ["Strawberries", "Fruits", 90, 1.20, 1.50] 16 | ] 17 | 18 | # create the workbook 19 | workbook = xlsxwriter.Workbook('Inventory.xlsx') 20 | worksheet = workbook.add_worksheet("Inventory") 21 | 22 | # Use the add_format function to define formats that you can use later 23 | # in the worksheet. NOTE: If you change the format then ALL prior instances 24 | # of the format will be saved as the most recent one 25 | 26 | 27 | # write the data into the workbook 28 | worksheet.write_row(0, 0, data[0]) 29 | for row, itemlist in enumerate(data[1:], start=1): 30 | worksheet.write(row, 0, itemlist[0]) 31 | worksheet.write(row, 1, itemlist[1]) 32 | worksheet.write(row, 2, itemlist[2]) 33 | worksheet.write(row, 3, itemlist[3]) 34 | worksheet.write(row, 4, itemlist[4]) 35 | 36 | 37 | worksheet.set_zoom(200) 38 | 39 | workbook.close() 40 | -------------------------------------------------------------------------------- /Start/Ch3/xlsx_formula_conditional.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter formulas and conditional formatting 4 | 5 | import csv 6 | import xlsxwriter 7 | 8 | 9 | def read_csv_to_array(filename): 10 | # define the array that will hold the data 11 | data = [] 12 | with open(filename, 'r') as csvfile: 13 | reader = csv.reader(csvfile) 14 | for row in reader: 15 | data.append(row) 16 | return data 17 | 18 | 19 | # Read the data into an array of arrays 20 | inventory_data = read_csv_to_array("Inventory.csv") 21 | 22 | # create the workbook 23 | workbook = xlsxwriter.Workbook('Conditional.xlsx') 24 | worksheet = workbook.add_worksheet("Inventory") 25 | 26 | fmt_bold = workbook.add_format({'bold': True}) 27 | fmt_money = workbook.add_format( 28 | {'font_color': 'green', 'num_format': '$#,##0.00'}) 29 | # define the format for the conditional expression 30 | 31 | 32 | # write the data into the workbook 33 | worksheet.write_row(0, 0, inventory_data[0], fmt_bold) 34 | # add the new header for the margin 35 | 36 | # add the data to the worksheet 37 | for row, itemlist in enumerate(inventory_data[1:], start=1): 38 | # worksheet.write_row(row, 0, itemlist) 39 | worksheet.write(row, 0, itemlist[0]) 40 | worksheet.write(row, 1, itemlist[1], fmt_bold) 41 | worksheet.write_number(row, 2, int(itemlist[2])) 42 | worksheet.write_number(row, 3, float(itemlist[3]), fmt_money) 43 | worksheet.write_number(row, 4, float(itemlist[4]), fmt_money) 44 | # calculate the row and column for the formula 45 | 46 | 47 | # add the conditional formatting 48 | 49 | 50 | worksheet.set_zoom(150) 51 | worksheet.autofit() 52 | 53 | workbook.close() 54 | -------------------------------------------------------------------------------- /Start/Ch3/xlsx_properties.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter document properties 4 | 5 | import xlsxwriter 6 | 7 | workbook = xlsxwriter.Workbook("Properties.xlsx") 8 | worksheet = workbook.add_worksheet() 9 | 10 | # set the standard properties 11 | 12 | 13 | # set some custom properties 14 | 15 | 16 | workbook.close() 17 | -------------------------------------------------------------------------------- /Start/Ch3/xlsx_tables.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # XlsxWriter Excel Tables 4 | 5 | 6 | import xlsxwriter 7 | 8 | # Sample data 9 | data = [ 10 | ["Item Name", "Category", "Quantity", "Wholesale Price", "Consumer Price"], 11 | ["Apple", "Fruits", 100, 0.50, 0.75], 12 | ["Banana", "Fruits", 150, 0.35, 0.50], 13 | ["Orange", "Fruits", 120, 0.45, 0.65], 14 | ["Grapes", "Fruits", 80, 0.60, 0.85], 15 | ["Strawberries", "Fruits", 90, 1.20, 1.50] 16 | ] 17 | 18 | # create the workbook 19 | workbook = xlsxwriter.Workbook('Tables.xlsx') 20 | worksheet = workbook.add_worksheet("Inventory") 21 | 22 | fmt_bold = workbook.add_format({'bold': True}) 23 | fmt_money = workbook.add_format( 24 | {'font_color': 'green', 'num_format': '$#,##0.00'}) 25 | 26 | # write the data into the workbook 27 | worksheet.write_row(0, 0, data[0], fmt_bold) 28 | for row, itemlist in enumerate(data[1:], start=1): 29 | # worksheet.write_row(row, 0, itemlist) 30 | worksheet.write(row, 0, itemlist[0]) 31 | worksheet.write(row, 1, itemlist[1], fmt_bold) 32 | worksheet.write(row, 2, itemlist[2]) 33 | worksheet.write(row, 3, itemlist[3], fmt_money) 34 | worksheet.write(row, 4, itemlist[4], fmt_money) 35 | 36 | # define a table for the worksheet 37 | 38 | 39 | worksheet.set_zoom(200) 40 | worksheet.autofit() 41 | 42 | workbook.close() 43 | -------------------------------------------------------------------------------- /Start/Ch4/FinancialSample.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-working-with-spreadsheet-and-excel-data-4554075/98b87f38e4728fef58842387541e41d56021f735/Start/Ch4/FinancialSample.xlsx -------------------------------------------------------------------------------- /Start/Ch4/Inventory.csv: -------------------------------------------------------------------------------- 1 | Item Name,Category,Quantity,Wholesale Price,Consumer Price 2 | Apple,Fruits,100,0.50,0.75 3 | Banana,Fruits,150,0.35,0.50 4 | Orange,Fruits,120,0.45,0.65 5 | Grapes,Fruits,80,0.60,0.85 6 | Strawberries,Fruits,90,1.20,1.50 7 | Carrot,Vegetables,200,0.30,0.45 8 | Broccoli,Vegetables,180,0.50,0.75 9 | Tomato,Vegetables,150,0.40,0.60 10 | Potato,Vegetables,220,0.25,0.35 11 | Onion,Vegetables,180,0.30,0.45 12 | Chicken,Meats,100,2.50,3.50 13 | Beef,Meats,80,3.00,4.50 14 | Pork,Meats,120,2.75,4.00 15 | Salmon,Meats,90,4.50,6.00 16 | Turkey,Meats,70,3.25,4.75 17 | White Bread,Breads,150,1.25,1.75 18 | Whole Wheat Bread,Breads,130,1.50,2.00 19 | Baguette,Breads,100,1.75,2.25 20 | Croissant,Breads,120,1.00,1.50 21 | Brioche,Breads,90,1.75,2.25 22 | Milk,Dairy,200,1.00,1.50 23 | Cheese,Dairy,180,2.00,3.00 24 | Yogurt,Dairy,220,0.75,1.25 25 | Butter,Dairy,150,1.50,2.25 26 | Eggs,Dairy,250,1.25,1.75 27 | Pear,Fruits,110,0.55,0.80 28 | Pineapple,Fruits,130,0.75,1.00 29 | Watermelon,Fruits,90,2.00,2.50 30 | Kiwi,Fruits,120,0.70,0.95 31 | Avocado,Fruits,100,1.25,1.75 32 | Lettuce,Vegetables,160,0.40,0.60 33 | Cucumber,Vegetables,190,0.35,0.50 34 | Bell Pepper,Vegetables,140,0.60,0.85 35 | Spinach,Vegetables,170,0.55,0.80 36 | Zucchini,Vegetables,150,0.45,0.65 37 | Lamb,Meats,80,4.00,5.50 38 | Veal,Meats,100,3.50,5.00 39 | Duck,Meats,90,3.75,5.25 40 | Shrimp,Meats,120,5.00,7.00 41 | Sausages,Meats,110,2.00,3.00 42 | Sourdough Bread,Breads,140,1.50,2.00 43 | Rye Bread,Breads,120,1.40,1.90 44 | Multigrain Bread,Breads,110,1.60,2.10 45 | Bagel,Breads,130,1.20,1.70 46 | Ciabatta,Breads,100,1.80,2.30 47 | Cream,Dairy,180,1.75,2.50 48 | Cottage Cheese,Dairy,160,1.25,1.75 49 | Sour Cream,Dairy,190,1.00,1.50 50 | Heavy Cream,Dairy,170,2.00,2.75 51 | Whipped Cream,Dairy,150,2.25,3.00 52 | -------------------------------------------------------------------------------- /Start/Ch4/challenge_start.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Challenge solution for working with Pandas data 4 | 5 | 6 | import pandas as pd 7 | 8 | -------------------------------------------------------------------------------- /Start/Ch4/pandas_explore.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Exploring DataFrame data and structure 4 | 5 | import pandas as pd 6 | import locale 7 | 8 | 9 | df = pd.read_excel("FinancialSample.xlsx") 10 | 11 | # Set the display options for Pandas 12 | 13 | 14 | # Examine the structure of the data set 15 | 16 | 17 | # View a subset of the data 18 | 19 | 20 | # Get the number of values for a given column 21 | 22 | 23 | # Get the unique data values for a column 24 | 25 | 26 | # Get the min and max values of a column 27 | 28 | 29 | # use the loc() function to conditionally sum a column 30 | 31 | 32 | # Get the sum of the Profit column for each of the Product types 33 | # and format the output as currency in the user's locale 34 | -------------------------------------------------------------------------------- /Start/Ch4/pandas_manipulate.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Manipulating DataFrame content 4 | 5 | import pandas as pd 6 | 7 | 8 | df = pd.read_csv("Inventory.csv") 9 | print(df.shape) 10 | 11 | # Create a new column of data 12 | 13 | 14 | # Modify a column in-place 15 | 16 | 17 | # rename a column in-place 18 | 19 | 20 | # Drop a column 21 | -------------------------------------------------------------------------------- /Start/Ch4/pandas_read.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Using Pandas library to read CSV and Excel data 4 | 5 | import pandas as pd 6 | 7 | 8 | # read a CSV file using read_csv 9 | 10 | 11 | # read just a portion of the file 12 | 13 | 14 | # read an Excel file 15 | 16 | 17 | # Get information about a workbook 18 | 19 | -------------------------------------------------------------------------------- /Start/Ch4/pandas_write.py: -------------------------------------------------------------------------------- 1 | # LinkedIn Learning Course 2 | # Example file for Python: Working with Excel and Spreadsheet Data by Joe Marini 3 | # Using Pandas library to write CSV and Excel data 4 | 5 | import pandas as pd 6 | 7 | # create a CSV file with Pandas 8 | data = [ 9 | {"Item Name":"Apple", "Category":"Fruits", "Quantity":100, "Wholesale Price":0.50, "Consumer Price":0.75}, 10 | {"Item Name":"Banana", "Category":"Fruits", "Quantity":150, "Wholesale Price":0.35, "Consumer Price":0.50}, 11 | {"Item Name":"Orange", "Category":"Fruits", "Quantity":120, "Wholesale Price":0.45, "Consumer Price":0.65}, 12 | {"Item Name":"Grapes", "Category":"Fruits", "Quantity":80, "Wholesale Price":0.60, "Consumer Price":0.85}, 13 | {"Item Name":"Strawberries", "Category":"Fruits", "Quantity":90, "Wholesale Price":1.20, "Consumer Price":1.50} 14 | ] 15 | 16 | 17 | # Convert a CSV file to an Excel file 18 | 19 | 20 | # append data to an existing workbook without overwriting 21 | 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests>=2.28 2 | --------------------------------------------------------------------------------