Image to DOCX
Overview
Convert images containing text and forms into properly formatted Word documents. Read the image to extract its content, then generate a .docx file using the docx-js library. Common use cases include scanned government forms, meeting minutes photos, receipts, signage, or any text-heavy image that needs to become an editable document.
Workflow
Step 1: Read and Analyze the Image
Read the image file using the Read tool to understand its content, layout, and structure. Identify:
- Title and headings
- Body text and paragraphs
- Tables or form fields
- Signature/date areas
- Any fill-in-the-blank fields (underlines)
- Alignment and formatting cues
Step 2: Set Up the docx-js Environment
The isolated Node.js environment is at C:Users86150.workbuddybinariesnode. The docx package must be installed in the workspace:
# Ensure workspace directory exists
mkdir -p /c/Users/86150/.workbuddy/binaries/node/workspace
# Install docx if not present
cd /c/Users/86150/.workbuddy/binaries/node/workspace && npm install docx
To verify installation:
ls /c/Users/86150/.workbuddy/binaries/node/workspace/node_modules/docx/package.json
Step 3: Write the Generation Script
Create a JavaScript file using the docx-js API. Use C:Users86150.workbuddybinariesnodeversions22.22.2node.exe as the Node runtime.
Useful patterns:
A4 page settings:
page: {
size: { width: 11906, height: 16838 }, // A4 in DXA
margin: { top: 1440, right: 1440, bottom: 1440, left: 1800 }
}
Chinese font (仿宋):
const font = "仿宋";
const t = (text, bold = false, size = 24) => new TextRun({
text, bold, size, font
});
Underline for fill-in blanks:
const u = (text) => new TextRun({
text,
font: "仿宋",
size: 24,
underline: { type: UnderlineType.SINGLE }
});
Centered title:
new Paragraph({
alignment: AlignmentType.CENTER,
children: [new TextRun({ text: "Title", bold: true, size: 28, font: "仿宋" })],
spacing: { after: 400 }
})
Body paragraph (Chinese indentation):
new Paragraph({
children: [new TextRun({
text: " 这是一个开头缩进的正文段落。",
font: "仿宋", size: 24
})],
spacing: { after: 200 }
})
Step 4: Run the Script and Generate DOCX
Execute the script by setting NODE_PATH to the workspace node_modules:
NODE_PATH="C:\Users\86150\.workbuddy\binaries\node\workspace\node_modules"
/c/Users/86150/.workbuddy/binaries/node/versions/22.22.2/node.exe
"<script-path>" 2>&1
The script should write the .docx to the current workspace directory and print "Done" on success.
Step 5: Present the Result
After generation succeeds, call present_files with the .docx file path. Keep the temp script file for reference but always present the final .docx to the user.
Notes
- The docx-js default page size is A4, not US Letter. Set explicitly for consistency.
- For Chinese documents, 仿宋 at 24 half-points (12pt) is standard for body text.
- Underlines for blanks use full-width spaces ( ) to maintain proper visual width.
- All file paths in scripts should use absolute paths to avoid cwd issues.
- Clean up temp scripts only after user confirms satisfaction with the output.




