Evaluating bids

Design a simple code that imports excel data to a word template

Published on the October 14, 2024 in IT & Programming

About this project

Open

I want a code that imports data such as a text that will be  used to name the created file name, the date, and such things.

I want the code like the one chatgpt made:
import pandas as pd
from docx import Document

# Step 1: Read the Excel log from your provided file path
file_path = r'C:\Users\thebl\OneDrive\Desktop\TestExcel.xlsx'  # Excel file path
df = pd.read_excel(file_path)

# Step 2: Define the starting row and the number of rows to process
start_row = 5  # Replace this with the starting row (e.g., Row index 5)
num_rows = 10  # Replace this with the number of rows you want to process

# Function to replace placeholders in the document
def replace_placeholders(doc, placeholders):
    # Loop through each paragraph and replace placeholders
    for paragraph in doc.paragraphs:
        for placeholder, value in placeholders.items():
            if placeholder in paragraph.text:
                paragraph.text = paragraph.text.replace(placeholder, str(value))

# Step 3: Loop through a specified number of rows starting from 'start_row'
for index, row in df.iloc[start_row:start_row + num_rows].iterrows():
    # Step 4: Extract relevant columns from the row
    file_name = row['F']        # Column F: RFI (Word file name)
    rfi_date = row['G']        # Column G: RFI date
    document_text = row['I']    # Column I: Word document text
    block_number = row['J']    # Column J: Block number
    building_number = row['K']  # Column K: Building number

    # Step 5: Open the template document from the provided path
    doc = Document(r'C:\Users\thebl\OneDrive\Desktop\TestWord.docx')  # Word template path

    # Step 6: Create a dictionary of placeholders and corresponding values
    placeholders = {
        '{RFI}': file_name,
        '{Date}': rfi_date,
        '{Text}': document_text,
        '{BlockNumber}': block_number,
        '{BuildingNumber}': building_number,
    }

    # Step 7: Replace placeholders in the document
    replace_placeholders(doc, placeholders)

    # Step 8: Save the new document using the value from Column F as the filename
    doc.save(f'C:\\Users\\thebl\\OneDrive\\Desktop\\{file_name}.docx')  # Save the file to the Desktop

print("Specified Word documents created successfully!")

Category IT & Programming
Subcategory Other
Project size Small
Is this a project or a position? Project
I currently have I have the design
Required availability As needed

Delivery term: Not specified

Skills needed