File size: 2,988 Bytes
902a6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import streamlit as st
import pandas as pd

# Inject custom CSS to style the buttons
st.markdown("""
    <style>
    .stButton>button {
        background-color: #4CAF50;
        color: white;
        width: 100%;
    }
    </style>
    """, unsafe_allow_html=True)

def excel_page():
    st.title(":green[Excel Data Format]")

    # Section 1: What is Excel?
    st.subheader("1. What is Excel?")
    st.write("""
    Excel is a spreadsheet tool for storing data in tabular format with rows and columns. 
    The data can be in various forms such as numeric values, text, dates, etc. 
    Excel files are commonly saved with `.xls` or `.xlsx` file extensions. 
    They are widely used in businesses for data analysis, reporting, and visualization.
    """)

    # Section 2: How to Read Excel Files
    st.subheader("2. How to Read Excel Files")
    st.write("""
    To read Excel files in Python, the `pandas` library is commonly used. 
    You can use `pd.read_excel()` function to load the Excel file into a DataFrame.
    """)

    st.code("""
import pandas as pd

# Read an Excel file
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')

# Display the data
print(df)
    """, language='python')

    # Section 3: Issues Encountered
    st.subheader("3. Issues Encountered When Handling Excel Files")
    st.write("""
    - **File not found**: If the file path is incorrect or the file does not exist.
    - **Sheet not found**: If the sheet name specified in `sheet_name` doesn't exist.
    - **Missing Libraries**: Sometimes, the necessary libraries for reading `.xlsx` files, such as `openpyxl` or `xlrd`, may be missing.
    """)

    # Section 4: How to Overcome These Errors/Issues
    st.subheader("4. How to Overcome These Errors/Issues")
    st.write("""
    - **File not found**: Make sure the file path is correct.
    - **Sheet not found**: Verify the sheet name or list all sheet names using `pd.ExcelFile('data.xlsx').sheet_names`.
    - **Missing Libraries**: Ensure all necessary libraries are installed using pip:
    ```bash
    pip install openpyxl xlrd
    ```
    - **Handle Missing File Error**: Catch errors using `try` and `except` block.
    """)

    st.code("""
# Handling File Not Found Error
try:
    df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
except FileNotFoundError:
    print("File not found. Check the file path.")

# List available sheet names
excel_file = pd.ExcelFile('data.xlsx')
print(excel_file.sheet_names)
    """, language='python')

    # Section 5: Downloadable Jupyter Notebook/PDF
    st.write("### Download the Code Example as a Jupyter Notebook or PDF")

    # Create a sample Jupyter notebook for download (for now, it's a basic example)
    notebook_content = """
# Excel Handling in Python

## What is Excel?
Excel is a spreadsheet tool for storing data in tabular format with rows and columns...

## How to Read Excel Files
```python
import pandas as pd

df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
print(df)