Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Write a python program which generate text documents using a doc file template from a…

  1. User

    Write a python program which generate text documents using a doc file template from a data taken from xls file

  2. Assistant

    To achieve this task, we'll use the python-docx library to work with the docx files and openpyxl to work with the excel files. First, you need to install these libraries using pip:

    pip install python-docx openpyxl
    

    Here is a sample program that reads data from an xls file and generates text documents using a docx template:

    import os
    from openpyxl import load_workbook
    from docx import Document
    
    def read_excel_data(file_path, sheet_name):
        workbook = load_workbook(file_path)
        sheet = workbook[sheet_name]
        data = []
    
        for row in sheet.iter_rows(values_only=True):
            data.append(row)
    
        return data[1:], data[0]
    
    def modify_template(doc_path, data_row, headers, output_dir):
        doc = Document(doc_path)
        file_name = ""
      
        for i, header in enumerate(headers):
            for paragraph in doc.paragraphs:
                if header in paragraph.text:
                    if header == "Name":
                        file_name = data_row[i].replace(" ", "_") + "_Document.docx"
                    paragraph.text = paragraph.text.replace(header, str(data_row[i]))
    
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
    
        doc.save(os.path.join(output_dir, file_name))
    
    def main():
        template_path = "template.docx"
        excel_file = "data.xlsx"
        sheet_name = "Sheet1"
        output_dir = "output"
    
        data_rows, headers = read_excel_data(excel_file, sheet_name)
    
        for data_row in data_rows:
            modify_template(template_path, data_row, headers, output_dir)
    
    if __name__ == "__main__":
        main()
    

    In this script:

    1. read_excel_data function reads the excel file data and returns the data rows and header row.
    2. modify_template function takes the DOCX template file path, the data row, headers, and output directory, then modifies the template using the data in the data row and saves the output to the given directory.
    3. In the main function, set the template path, the excel file path, the sheet name, and the output directory. Then, read the excel data, and iterate through the data rows generating the corresponding DOCX files using the template.

    You'll want to update the template_path, excel_file, sheet_name, and output_dir variables to match your specific setup. Additionally, make sure to use the appropriate placeholders in your docx template that match your xls file headers, like {Name}, {Address}, {Phone}, etc. for the script to replace with the actual values in the xls file.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.