Skip to content

Instantly share code, notes, and snippets.

@gboone
Created July 17, 2024 18:41
Show Gist options
  • Save gboone/364966762b8329b3238ad248cc5cb08f to your computer and use it in GitHub Desktop.
Save gboone/364966762b8329b3238ad248cc5cb08f to your computer and use it in GitHub Desktop.
from pptx import Presentation
import csv
# Function to extract table data from all slides in a PowerPoint file
def extract_all_table_data(pptx_file):
prs = Presentation(pptx_file)
all_table_data = []
for slide_idx, slide in enumerate(prs.slides):
slide_data = extract_table_data_from_slide(slide)
all_table_data.extend(slide_data) # Use extend to append inner lists
return all_table_data
# Function to extract table data from a single slide
def extract_table_data_from_slide(slide):
slide_data = []
for shape in slide.shapes:
if not shape.has_table:
continue
table = shape.table
for row_idx, row in enumerate(table.rows):
if row_idx == 0:
continue # Skip header row
row_data = []
for cell in row.cells:
row_data.append(cell.text_frame.text.strip())
slide_data.append(row_data)
return slide_data
# Function to write data to a unified CSV file
def write_to_csv(all_table_data, csv_file):
with open(csv_file, 'w', newline='', encoding='utf-8') as f:
csv_writer = csv.writer(f)
csv_writer.writerows(all_table_data)
print(f"All table data has been written to '{csv_file}'.")
# Example usage
if __name__ == "__main__":
pptx_file = './file.pptx' # Replace with your PowerPoint file path
csv_file = './file.csv' # Replace with path to the unified CSV file
all_table_data = extract_all_table_data(pptx_file)
write_to_csv(all_table_data, csv_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment