Python script to export attribute tables from multiple shapefiles in a folder to CSV format using ArcGIS Pro

Here is a Python script that you can use in ArcGIS Pro to export attribute tables from multiple shapefiles in a folder to CSV format. This script will work with ArcPy, which is the Python library that comes with ArcGIS Pro.

To use this script in ArcGIS Pro:

  1. Open ArcGIS Pro and go to the Python window (you can open it from the Analysis tab, or press Ctrl+Alt+P)
  2. Copy and paste the script.
  3. Modify the input and output folder paths in the example usage section:
    • input_shapefile_folder = r”C:\Path\To\Your\Shapefiles”
    • output_csv_folder = r”C:\Path\To\Save\CSV\Files”
  4. Run the script
import arcpy
import os

def export_shapefile_attributes_to_csv(input_folder, output_folder):
    """
    Exports attribute tables from all shapefiles in a folder to CSV format.
    
    Parameters:
    input_folder (str): Path to the folder containing shapefiles
    output_folder (str): Path to save the exported CSV files
    """
    # Check if output folder exists, create it if it doesn't
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        print(f"Created output folder: {output_folder}")
    
    # Set the workspace
    arcpy.env.workspace = input_folder
    
    # Get a list of all shapefiles in the folder
    shapefiles = arcpy.ListFeatureClasses("*", "ALL")
    
    if not shapefiles:
        print(f"No shapefiles found in {input_folder}")
        return
    
    print(f"Found {len(shapefiles)} shapefiles to process...")
    
    # Process each shapefile
    for shapefile in shapefiles:
        try:
            # Get the shapefile name without extension
            shapefile_name = os.path.splitext(shapefile)[0]
            
            # Define the output CSV path
            output_csv = os.path.join(output_folder, f"{shapefile_name}.csv")
            
            # Export the attribute table to CSV
            arcpy.TableToTable_conversion(
                in_rows=os.path.join(input_folder, shapefile),
                out_path=output_folder,
                out_name=f"{shapefile_name}.csv"
            )
            
            print(f"Successfully exported: {shapefile} → {output_csv}")
            
        except Exception as e:
            print(f"Error processing {shapefile}: {str(e)}")
    
    print("Processing complete!")

# Example usage
if __name__ == "__main__":
    # Replace these paths with your actual paths
    input_shapefile_folder = r"C:\Path\To\Your\Shapefiles"
    output_csv_folder = r"C:\Path\To\Save\CSV\Files"
    
    export_shapefile_attributes_to_csv(input_shapefile_folder, output_csv_folder)

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × three =

This site uses Akismet to reduce spam. Learn how your comment data is processed.