Fundamentals 5 min read

How to Import Custom Python Functions and Merge Excel Files Like a Pro

This guide shows how to package frequently used Python functions into importable modules, then demonstrates a complete script that merges multiple Excel files while preserving formatting, using pandas and standard libraries, and explains each step with screenshots and code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Import Custom Python Functions and Merge Excel Files Like a Pro

In many real‑world projects developers repeatedly copy‑paste the same code. Instead of rewriting functions each time, you can package them into a Python module and import them just like built‑in libraries.

Background

Often you need to combine all .xlsx files in a directory into a single workbook. The following function merges the files, keeps the original data format, and saves the result as data_ok.xlsx inside a filtered_data folder.

def concat_excels(pattern):
    import pandas as pd
    import os
    import glob

    if not os.path.exists('filtered_data'):
        os.mkdir('filtered_data')

    file_paths = glob.glob(pattern)
    df = pd.DataFrame()
    for file_path in file_paths:
        df_ = pd.read_excel(file_path)
        df = pd.concat([df, df_])

    df.to_excel('filtered_data/data_ok.xlsx', index=False)
    print('Finished!')

if __name__ == '__main__':
    concat_excels('*.xlsx')

Specific Steps

1. Locate the installation directory of the third‑party library you are using (e.g., requests) by running import requests, print(requests.__file__) in your editor; the highlighted path shows where to place your custom module.

Python library path
Python library path

2. Save the function in a .py file, give it a short name, and move the file into the highlighted directory.

Move custom module
Move custom module

3. In the folder containing the Excel files, create a new Python script (e.g., merge_data.py).

Create merge script
Create merge script

4. Write the import statement and call the function to perform the merge. The screenshot below shows the script running successfully. from 模块名 import * means import all functions from the module, allowing direct calls.

Import and run
Import and run

Extension

By storing frequently used utilities in your own module, you can reuse them across projects with a single import. For example, you can wrap image loading, color correction, and display logic into functions and call them with one line of code.

Image processing functions
Image processing functions

Demo screenshots illustrate the streamlined workflow.

Demo
Demo
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonpandasmodule importExcel merging
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.