Created
December 26, 2024 21:40
-
-
Save eveevans/0ac9d75d443e35635b1de2a72c1a4d9e to your computer and use it in GitHub Desktop.
Test 02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import openpyxl | |
def delete_rows_from_excel(file_path, rows_to_delete): | |
# Load the workbook and select the first sheet | |
workbook = openpyxl.load_workbook(file_path) | |
sheet = workbook.active | |
# Delete the specified rows | |
for row in sorted(rows_to_delete, reverse=True): | |
sheet.delete_rows(row) | |
# Save the updated workbook to a new file | |
new_file_path = file_path.replace('.xlsx', '_updated.xlsx') | |
workbook.save(new_file_path) | |
print(f"Rows deleted. Updated file saved as {new_file_path}") | |
def process_excel_files_in_folder(folder_path, rows_to_delete): | |
for file_name in os.listdir(folder_path): | |
if file_name.endswith('.xlsx'): | |
file_path = os.path.join(folder_path, file_name) | |
delete_rows_from_excel(file_path, rows_to_delete) | |
# Example usage | |
folder_path = 'path/to/your/folder' | |
rows_to_delete = [2, 4] # Rows to delete (1-based index) | |
process_excel_files_in_folder(folder_path, rows_to_delete) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install openpyxl | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment