如何按顺序将Excel击球手名单逐行写入并替换TXT文件?
Got it, let's break this down into simple, doable steps that don't require heavy coding experience—perfect for a youth league setup where you need something quick and reliable.
You’ve got two easy options here: using Excel’s built-in VBA (no extra software needed) or a lightweight Python script (great if you prefer more flexibility).
Option 1: Excel VBA (No Extra Tools)
Since you’re already working with Excel, this is the most straightforward approach:
- Open your batter Excel file, press
Alt + F11to open the VBA Editor. - Right-click your workbook in the Project pane > Insert > Module.
- Paste this code (adjust the paths and column/row references to match your sheet):
' Global variable to track which row we're on (starts at 2 assuming row 1 is headers) Dim currentRow As Integer Sub InitializeRowTracker() ' Run this once when you open the file to reset the tracker currentRow = 2 End Sub Sub LoadNextBatter() Dim batterName As String Dim txtFilePath As String ' Replace this with your desired TXT file location txtFilePath = "C:\YouthLeague\CurrentBatter.txt" ' Grab the batter name from column A (change "A" to your name column) batterName = ThisWorkbook.Sheets("Batters").Cells(currentRow, "A").Value ' Loop back to the first batter if we hit an empty row If batterName = "" Then currentRow = 2 batterName = ThisWorkbook.Sheets("Batters").Cells(currentRow, "A").Value End If ' Write the name to TXT and overwrite existing content Open txtFilePath For Output As #1 Print #1, batterName Close #1 ' Move to the next row for the next batter currentRow = currentRow + 1 End Sub
- Add a button to your Excel sheet for easy access:
- Go to the Developer tab > Insert > Button (Form Control).
- Draw the button on your sheet, then select the
LoadNextBattermacro when prompted. - Rename the button something like "Next Batter".
Now every time you click the button, it’ll update the TXT file with the next batter’s name.
Option 2: Python Script (Lightweight & Cross-Platform)
If you don’t want to mess with VBA, a simple Python script works great:
- Install the required packages (open Command Prompt/Terminal and run):
pip install pandas openpyxl
- Create a new file named
next_batter.pywith this code (adjust paths and column names):
import pandas as pd # Configure your file paths and column name EXCEL_FILE = "youth_league_batters.xlsx" TXT_OUTPUT = "CurrentBatter.txt" ROW_TRACKER = "current_row.txt" # Tracks our position in the Excel sheet BATTER_COLUMN = "Name" # Replace with your column header for batter names # Load the current row index (start at 1 for Excel row 2, since pandas uses 0-indexing) try: with open(ROW_TRACKER, "r") as f: current_idx = int(f.read().strip()) except FileNotFoundError: current_idx = 1 # Read the Excel sheet df = pd.read_excel(EXCEL_FILE, sheet_name="Sheet1") # Get the next batter (loop back to start if we reach the end) if current_idx >= len(df): current_idx = 1 batter_name = df.loc[current_idx, BATTER_COLUMN] # Write name to TXT (overwrite existing content) with open(TXT_OUTPUT, "w", encoding="utf-8") as f: f.write(batter_name) # Save the updated row index with open(ROW_TRACKER, "w") as f: f.write(str(current_idx + 1))
- Create a shortcut to this script (right-click > Send to > Desktop (create shortcut))—double-click it anytime to load the next batter.
For this, use OBS Studio (free, open-source, and standard for video overlays):
- Download and install OBS Studio.
- Add a "Text (Read from File)" source to your scene:
- In the Sources panel, click the
+icon > Text (Read from File). - Name it "Current Batter" and click OK.
- In the settings, click "Browse" and select your
CurrentBatter.txtfile. - Adjust the font, size, color, and position to fit your video stream.
- In the Sources panel, click the
- OBS will automatically detect when the TXT file updates and refresh the on-screen text instantly—no manual reloading needed.
Putting it all together: Every time a new batter steps up, click the Excel button or double-click the Python script, and OBS will show the new name on your video stream. Super straightforward for game day!
内容的提问来源于stack exchange,提问作者Chris Foster

