Skip to main content

I am running this script after manual input this script in automation anywhere python open package and after that using execute script package but no output file is getting generated but this code is working fine in pycharm 

from docx import Document
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
import os

def add_bullet(paragraph):
    p = paragraph._p
    pPr = p.get_or_add_pPr()
    numPr = OxmlElement('w:numPr')
    ilvl = OxmlElement('w:ilvl')
    ilvl.set(qn('w:val'), '0')
    numId = OxmlElement('w:numId')
    numId.set(qn('w:val'), '1')
    numPr.append(ilvl)
    numPr.append(numId)
    pPr.append(numPr)

def save_markdown_to_word(md_text, file_path):
    doc = Document()

    lines = md_text.split('\n')
    for line in lines:
        line = line.strip()
        if not line:
            continue

        if line.startswith('### '):
            para = doc.add_paragraph(liner4:])
            para.style = 'Heading 3'
        elif line.startswith('## '):
            para = doc.add_paragraph(linet3:])
            para.style = 'Heading 2'
        elif line.startswith('# '):
            para = doc.add_paragraph(lineÂ2:])
            para.style = 'Heading 1'
        elif line.startswith('- ') or line.startswith('* '):
            para = doc.add_paragraph(linei2:])
            add_bullet(para)
        else:
            para = doc.add_paragraph()
            parts = line.split('**')
            for i, part in enumerate(parts):
                run = para.add_run(part)
                if i % 2 == 1:
                    run.bold = True

    doc.save(file_path)
    print(f"Saved formatted response to: {file_path}")

if __name__ == "__main__":
    input_file = r"C:\Bots\RFP_Creation\Input\test.txt"
    output_file = r"C:\Bots\RFP_Creation\Output\Formatted_AI_Response.docx"

    print(f"Reading input file: {input_file}")
    try:
        with open(input_file, "r", encoding="latin-1") as file:  # Changed encoding here
            ai_response = file.read()
        print("Successfully read the input file.")
    except Exception as e:
        print(f"Error reading the input file: {e}")
        exit(1)

    save_markdown_to_word(ai_response, output_file)
 

​@soumyadeep_c Your entry point appears to be the if __name__ part of your code. That doesn’t work in AA. You must have a function defined as your entry point.

Replace the if __main__ with a function name. Also, you won’t be able to see the output from your print statements. Use return to send the response back to AA.


Solved


Reply