GroupDocs.Merger for Python via .NET lets you split a plain-text file into multiple output files based on line numbers. You can extract specific lines (one output per listed line) or split at line boundaries to produce multi-line segments — all controlled through TextSplitOptions.
Here are the steps to split a text file:
Instantiate Merger with the path to the source .txt file.
Create a TextSplitOptions object using keyword arguments — file_path_format, line_numbers, and mode.
Call merger.split() passing the TextSplitOptions object.
Warning
TextSplitOptions must be constructed with keyword arguments. Passing arguments positionally will fail at runtime.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportTextSplitOptions,TextSplitModedefsplit_text_file_by_lines():# Load the source text filewithMerger("./input.txt")asmerger:# Split at lines 2 and 4 — each listed line becomes a separate output file# Output files: lines_0.txt (line 2), lines_1.txt (line 4)# IMPORTANT: use keyword arguments — positional arguments will failmerger.split(TextSplitOptions(file_path_format="./lines_{0}.txt",line_numbers=[2,4],mode=TextSplitMode.LINES,))defsplit_text_file_by_interval():# Load the source text filewithMerger("./input.txt")asmerger:# Split at line boundaries [2, 4] using INTERVAL mode# lines_0.txt → line 1, lines_1.txt → lines 2-3, lines_2.txt → lines 4+merger.split(TextSplitOptions(file_path_format="./lines_{0}.txt",line_numbers=[2,4],mode=TextSplitMode.INTERVAL,))if__name__=="__main__":split_text_file_by_lines()split_text_file_by_interval()
input.txt is a sample file used in this example. Click here to download it.
Load source file: Merger("./input.txt") opens the text file as a context manager.
TextSplitMode.LINES: each number in line_numbers identifies a single line to extract. The result is one output file per listed line number.
TextSplitMode.INTERVAL: the listed numbers are treated as boundary points. Lines before the first boundary form the first segment, lines between each pair of boundaries form subsequent segments, and lines after the last boundary form the final segment.
Keyword arguments: file_path_format, line_numbers, and mode must always be passed as keyword arguments to TextSplitOptions. Positional usage is not supported and raises a runtime error.
Output format: {0} in the path format is replaced with a zero-based index for each output file.
No explicit merger.save() call is needed — split() writes the output files directly.