Fundamentals 4 min read

Importing Custom Python Functions as Modules and Merging Multiple Excel Files

This tutorial explains how to package reusable Python functions into importable modules and provides a step‑by‑step guide, including code, to merge multiple Excel files into a single workbook using pandas, enabling easier reuse and automation of common data‑processing tasks.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Importing Custom Python Functions as Modules and Merging Multiple Excel Files

Background

In many work scenarios we repeatedly write the same functions or copy‑paste code, which is cumbersome. This article shows how to package frequently used functions into a Python module that can be imported like any standard library, and demonstrates merging multiple Excel files into a single workbook.

Specific Steps

Step 1: Locate the installation directory of third‑party libraries used by your editor (e.g., the path of the requests package) and note the folder shown in red.

Step 2: Save the function code into a .py file named with a short identifier and move this file into the folder identified in step 1.

Step 3: In the directory containing the Excel files to be merged, create a new Python script (e.g., merge_data.py ).

Step 4: Write the following code, import the custom module, and run it to produce filtered_data/data_ok.xlsx . The code also prints “Finished!”.

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')

Extension

By placing commonly used utilities in your own module, you can import them with from module_name import * and call functions directly. Adding new utilities is as simple as editing the .py file and copying the code.

For example, image‑processing functions (color correction, display) can be encapsulated and invoked with a single line, as illustrated in the screenshots.

Original source: https://blog.csdn.net/lys_828/article/details/106176229

AutomationData ProcessingExcelpandasmodule import
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.