import pandas as pd from docx import Document import os from datetime import datetime…
- User
import pandas as pd from docx import Document import os from datetime import datetime
def auto_populate_chambers_form(excel_file, template_file, output_file): """ Auto-populate Chambers submission form (Word document) with data from Excel file
Parameters: excel_file (str): Path to the Excel file containing matter data template_file (str): Path to the Word document template (Chambers.docx) output_file (str): Path where the populated document will be saved """ print(f"Reading data from Excel file: {excel_file}") # Read Excel data df = pd.read_excel(excel_file) # Load the Word document template doc = Document(template_file) # Count how many publishable and confidential matters we have publishable_matters = df[df['Confidential/Publishable'].str.lower() == 'publishable'] confidential_matters = df[df['Confidential/Publishable'].str.lower() == 'confidential'] print(f"Found {len(publishable_matters)} publishable matters and {len(confidential_matters)} confidential matters") # Populate publishable matters (Section D) for i, matter in publishable_matters.iterrows(): if i >= 10: # Max 10 publishable matters allowed print("Warning: More than 10 publishable matters found in Excel. Only the first 10 will be used.") break matter_num = i + 1 section_name = f"Publishable Matter {matter_num}" # Find the section in the document found_section = False for para in doc.paragraphs: if section_name in para.text: found_section = True break if not found_section: print(f"Warning: Could not find section '{section_name}' in the template document.") continue # Populate fields for this matter populate_matter_fields( doc, section_name, "D", matter['Client Name'], matter['Matter/Case Overview'], matter['Value'], matter.get('Cross-border', 'N/A'), matter['Lead partners'], matter['Team Members'], matter.get('Other Firms/counsels Involved', ''), matter.get('Current Status', ''), matter.get('Additional Info', '') ) # Populate confidential matters (Section E) for i, matter in confidential_matters.iterrows(): if i >= 10: # Max 10 confidential matters allowed print("Warning: More than 10 confidential matters found in Excel. Only the first 10 will be used.") break matter_num = i + 1 section_name = f"Confidential Matter {matter_num}" # Find the section in the document found_section = False for para in doc.paragraphs: if section_name in para.text: found_section = True break if not found_section: print(f"Warning: Could not find section '{section_name}' in the template document.") continue # Populate fields for this matter populate_matter_fields( doc, section_name, "E", matter['Client Name'], matter['Matter/Case Overview'], matter['Value'], matter.get('Cross-border', 'N/A'), matter['Lead partners'], matter['Team Members'], matter.get('Other Firms/counsels Involved', ''), matter.get('Current Status', ''), matter.get('Additional Info', '') ) # Populate client lists populate_client_lists(doc, publishable_matters, confidential_matters) # Save the populated document doc.save(output_file) print(f"Document successfully populated and saved as: {output_file}") - Assistant
- User
S No. Client Name Matter/Case Overview Value Description (in 500 words) Role of S&A The significance, impact and outcome of the case Current Status (ongoing / completed ) Lead partners Team Members Other Firms/counsels Involved Confidential / Publishable these are the columns
- Assistant
- User
PS C:\Users\Administrator\Desktop\Bd team> python app.py Reading data from Excel file: C:\Users\Administrator\Documents\Submissions- Arbitration (version 1).xlsb.xlsx Columns in DataFrame: ['S No.', 'Client Name', 'Matter/Case Overview', 'Value', 'Description (in 500 words)', 'Role of S&A', 'The significance, impact and outcome of the case', 'Current Status (ongoing / completed )', 'Lead partners', 'Team Members', 'Other Firms/counsels Involved', 'Confidential / Publishable'] Found 0 publishable matters and 0 confidential matters Traceback (most recent call last): File "C:\Users\Administrator\Desktop\Bd team\app.py", line 239, in auto_populate_chambers_form(excel_file, template_file, output_file) File "C:\Users\Administrator\Desktop\Bd team\app.py", line 110, in auto_populate_chambers_form populate_client_lists(doc, publishable_matters, confidential_matters) ^^^ NameError: name 'doc' is not defined No in last column means publishable and yes is confidential
- Assistant