python给word插入脚注

1.需求

最近因为工作需要,需要给大量文本的脚注插入内容,我就写了个小程序。

2.实现

下面程序是我已经给所有脚注插入了两次文本"幸福",给脚注2到4再插入文本"幸福"

复制代码
from win32com import client


def add_text_to_specific_footnotes(filename, start_index, end_index, text_to_add):
    word = client.Dispatch('Word.Application')
    doc = word.Documents.Open(filename)

    try:
        # 遍历指定范围内的脚注索引
        for footnote_index in range(start_index, end_index + 1):
            try:
                # 获取当前索引的脚注
                target_footnote = doc.Footnotes.Item(footnote_index)
                # 直接使用脚注的Range
                r = target_footnote.Range
                # 在脚注中添加文字
                r.InsertAfter(text_to_add)
            except Exception as e:
                print(f"Error adding text to footnote {footnote_index}: {e}")

        # 保存文档
        doc.Save()

    except Exception as e:
        print(f"Error: {e}")

    finally:
        # 关闭文档和 Word 应用程序
        doc.Close()
        word.Quit()


# 示例:在第2到第4个脚注上添加文本
add_text_to_specific_footnotes(r'C:/Users/zhang/Desktop/521.docx', 2, 4, "幸福")

输出的文本插入脚注结果:

文本

脚注

3.这是读取文本内容,然后再插入到脚注的程序

复制代码
from win32com import client
from docx import Document

def read_word_document(file_path):
    doc = Document(file_path)
    paragraphs_list = []

    for paragraph in doc.paragraphs:
        paragraphs_list.append(paragraph.text)

    return paragraphs_list


def add_text_to_specific_footnotes(filename, start_index, end_index, texts_to_add):
    word = client.Dispatch('Word.Application')
    doc = word.Documents.Open(filename)

    try:
        # Check if the length of texts_to_add is equal to the number of footnotes
        if len(texts_to_add) != (end_index - start_index + 1):
            print("Error: Number of texts does not match the number of footnotes.")
            return

        # 遍历指定范围内的脚注索引
        for i, footnote_index in enumerate(range(start_index, end_index + 1)):
            try:
                # 获取当前索引的脚注
                target_footnote = doc.Footnotes.Item(footnote_index)
                # 直接使用脚注的Range
                r = target_footnote.Range
                # 在脚注中添加文字
                r.InsertAfter(texts_to_add[i])
            except Exception as e:
                print(f"Error adding text to footnote {footnote_index}: {e}")

        # 保存文档
        doc.Save()

    except Exception as e:
        print(f"Error: {e}")

    finally:
        # 关闭文档和 Word 应用程序
        doc.Close()
        word.Quit()

# 示例:在第2到第4个脚注上添加文本
# 替换 'your_word_document.docx' 为你的Word文档的文件路径
file_path0 = 'C:/Users/zhang/Desktop/test_python_插入脚注/脚注内容.docx'
paragraphs = read_word_document(file_path0)

length = len(paragraphs)
print(length)
texts_to_add = [paragraphs[0],paragraphs[1],paragraphs[2]]
add_text_to_specific_footnotes(r'C:/Users/zhang/Desktop/test_python_插入脚注/521.docx', 1, length,paragraphs)
相关推荐
你怎么知道我是队长1 分钟前
C语言---存储类
c语言·开发语言
XIAOYU6720137 分钟前
金融数学专业需要学哪些数学和编程内容?
开发语言·matlab·金融
油炸自行车10 分钟前
【Qt】编写Qt自定义Ui控件步骤
开发语言·c++·qt·ui·自定义ui控件·qt4 自定义ui控件
夏日麋鹿~13 分钟前
逐时nc数据批量处理为日平均
python
程序员三明治18 分钟前
Python编辑器的安装及配置(Pycharm、Jupyter的安装)从0带你配置,小土堆视频
python·pycharm·编辑器
理想国的女研究僧19 分钟前
Jupyter Notebook操作指南(1)
ide·python·学习·jupyter
酷飞飞27 分钟前
PyQt 界面布局与交互组件使用指南
python·qt·交互·pyqt
浪扼飞舟32 分钟前
c#基础二(类和对象,构造器调用顺序、访问级别、重写和多态、抽象类和接口)
java·开发语言·c#
GilgameshJSS1 小时前
【学习K230-例程19】GT6700-TCP-Client
网络·python·网络协议·学习·tcp/ip
yuanpan1 小时前
python标准库有哪些模块,简单总结下。
开发语言·python