import pandas as pd from docx import Document from docx.shared import RGBColor import os…
- User
import pandas as pd from docx import Document from docx.shared import RGBColor import os from docx.oxml.ns import qn
def populate_chambers(excel_path, doc_path_yes, doc_path_no, output_path): try: print("Loading Excel data...") df = pd.read_excel(excel_path) print(f"Excel data loaded: {len(df)} rows")
# Create directories for temporary files if they don't exist temp_dir = os.path.join(os.path.dirname(output_path), "temp") os.makedirs(temp_dir, exist_ok=True) yes_files = [] no_files = [] # Iterate through each row of the DataFrame for i, row in df.iterrows(): # Determine which document to use based on the Confidential/Publishable column confidential_val = str(row.get('Confidential / Publishable ', '')).strip() if confidential_val == 'Yes': # Create a new document from the template for each row doc = Document(doc_path_yes) if doc.tables: table = doc.tables[0] # Use the first table in the template fill_table_with_data(row, table, True) # Save as temporary file temp_file = os.path.join(temp_dir, f"yes_{i}.docx") doc.save(temp_file) yes_files.append(temp_file) print(f"Created 'Yes' document for row {i+1}") else: print(f"Warning: No tables found in the 'Yes' template.") elif confidential_val == 'No': # Create a new document from the template for each row doc = Document(doc_path_no) if doc.tables: table = doc.tables[0] # Use the first table in the template fill_table_with_data(row, table, False) # Save as temporary file temp_file = os.path.join(temp_dir, f"no_{i}.docx") doc.save(temp_file) no_files.append(temp_file) print(f"Created 'No' document for row {i+1}") else: print(f"Warning: No tables found in the 'No' template.") else: print(f"Warning: Unexpected value in the 'Confidential / Publishable ' column: {confidential_val}.") # Combine all documents: 'No' files followed by 'Yes' files combine_word_documents(no_files + yes_files, output_path) print(f"All documents combined successfully to {output_path}!") # Clean up temporary files for file in no_files + yes_files: try: os.remove(file) except Exception as e: print(f"Warning: Could not delete temporary file {file}: {e}") except Exception as e: print(f"Unexpected error: {e}")def fill_table_with_data(row, table, is_confidential): try: # Using the exact same cell references as your original code if len(table.rows) >= 25: # Make sure the table has enough rows # Fill the cells from the row in the DataFrame using your original cell references fill_cell(table.cell(4, 0), str(row['Client Name ']), is_confidential) # Client Name fill_cell(table.cell(24, 0), str(row['Description (in 500 words)']), is_confidential) # Description fill_cell(table.cell(14, 0), str(row['Value ']), is_confidential) # Matter/Case Overview fill_cell(table.cell(20, 0), str(row['Lead partners']), is_confidential) # Lead Partner fill_cell(table.cell(22, 0), str(row['Other Firms/counsels Involved']), is_confidential) # Other Involved Firms
- Assistant
- User
Upadted full code
- Assistant
- User
- Assistant
- User
they all are togrther at starting ...all empty
- Assistant