You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python docx报错:'Document' object is not subscriptable 问题求助

问题:使用python-docx生成求职信时出现Document对象下标错误

我正在学习Python,用python-docx库生成求职信时碰到问题——第一部分代码正常运行,但添加筛选零售及营销职位的逻辑后,出现了Document对象相关错误。检查过文件路径和循环逻辑,也试过把if改成elif,都没解决。附上代码和完整报错信息,求解答。

import docx
for jobs in job_details:
    finance_cl_template = docx.Document('D:\\Desktop\\cl automator\\finance cl template.docx')
    if "(Banking & Financial Services)" in jobs['category']:
        finance_cl_template.paragraphs[2].text = finance_cl_template.paragraphs[2].text.replace('OFD', jobs['title'])
        finance_cl_template.paragraphs[3].text = finance_cl_template.paragraphs[3].text.replace('PGA', jobs['title']).replace('ACC', jobs['recruiter'])
        finance_cl_template.paragraphs[12].text = finance_cl_template.paragraphs[12].text.replace('ACC', jobs['recruiter'])
        finance_cl_template.save(f'D:\\Desktop\\cl automator\\{jobs["title"]} Cover Letter.docx')
    if "(Retail & Consumer Products)" in jobs['category'] or "(Marketing & Communications)" in jobs['category']:
        retail_and_marketing_cl_template = docx.Document('D:\\Desktop\\cl automator\\retail and marketing cl template.docx')
        retail_and_marketing_cl_template[2].text = retail_and_marketing_cl_template.paragraphs[2].text.replace('ZFG', jobs['title'])
        retail_and_marketing_cl_template[5].text = retail_and_marketing_cl_template.paragraphs[5].text.replace('XYZ', jobs['title'])
        retail_and_marketing_cl_template[12].text = retail_and_marketing_cl_template.paragraphs[12].text.replace('ABC', jobs['recruiter'])
        retail_and_marketing_cl_template.save(f'D:\\Desktop\\cl automator\\{jobs["title"]} Cover Letter.docx')

完整报错信息:

Traceback (most recent call last):
  File "D:/Desktop/Python/AI seek cover letter scraper word part3.py", line 130, in <module>
    retail_and_marketing_cl_template[2].text = retail_and_marketing_cl_template.paragraphs[2].text.replace('Spark Pukekohe', jobs['title'])
TypeError: 'Document' object is not subscriptable

问题原因及修复方案

报错的核心原因是:Document对象本身不支持下标访问,你在操作零售营销模板时,错误地直接用retail_and_marketing_cl_template[2]来获取段落,正确的方式是通过.paragraphs属性获取段落列表后,再用下标访问具体段落。

修复后的代码如下:

import docx
for jobs in job_details:
    finance_cl_template = docx.Document('D:\\Desktop\\cl automator\\finance cl template.docx')
    if "(Banking & Financial Services)" in jobs['category']:
        finance_cl_template.paragraphs[2].text = finance_cl_template.paragraphs[2].text.replace('OFD', jobs['title'])
        finance_cl_template.paragraphs[3].text = finance_cl_template.paragraphs[3].text.replace('PGA', jobs['title']).replace('ACC', jobs['recruiter'])
        finance_cl_template.paragraphs[12].text = finance_cl_template.paragraphs[12].text.replace('ACC', jobs['recruiter'])
        finance_cl_template.save(f'D:\\Desktop\\cl automator\\{jobs["title"]} Cover Letter.docx')
    if "(Retail & Consumer Products)" in jobs['category'] or "(Marketing & Communications)" in jobs['category']:
        retail_and_marketing_cl_template = docx.Document('D:\\Desktop\\cl automator\\retail and marketing cl template.docx')
        # 修正:添加.paragraphs属性
        retail_and_marketing_cl_template.paragraphs[2].text = retail_and_marketing_cl_template.paragraphs[2].text.replace('ZFG', jobs['title'])
        retail_and_marketing_cl_template.paragraphs[5].text = retail_and_marketing_cl_template.paragraphs[5].text.replace('XYZ', jobs['title'])
        retail_and_marketing_cl_template.paragraphs[12].text = retail_and_marketing_cl_template.paragraphs[12].text.replace('ABC', jobs['recruiter'])
        retail_and_marketing_cl_template.save(f'D:\\Desktop\\cl automator\\{jobs["title"]} Cover Letter.docx')

另外补充一个小优化:可以把模板文件路径定义成变量,避免重复写长路径,提升代码可读性。

内容的提问来源于stack exchange,提问作者vee2gee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:53:23