python-docx的应用

记录

一、安装pytho=docx库

         python-docs 官方文档 python-docx — python-docx 1.1.2 文档

   pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple python-docx

二、实现功能

查找指定路径下后缀为docx所文件内的某个字符串,并输出相关信息。

实例

 

# -- coding: utf-8 --

import os

import glob

from docx import Document


def find_string_indocx(file_path,string):

    doc = Document(file_path)

    for paragraph in doc.paragraphs:

        if string in paragraph.text:

            print(paragraph.text);
            #paragraph.text = paragraph.text.replace(old_string, new_string)//替换功能

    for table in doc.tables:

        for row in table.rows:

            for cell in row.cells:

                if string in cell.text:

                    for cell2 in row.cells:

                        print(cell2.text, end=' ')

                    print()
    #doc.save(file_path)




folder_path = "/mnt/hgfs/ubuntu_free_dir/dir_work/补充文档/" #将此处替换为你的.docx文件所在的文件夹路径

'''

old_string = "大师傅打发环境是否开放花萼法务"

new_string = "测试输出新字符"

'''

while True:

    string_find = "测试输出新字符"

    string_find = input("请输入你要寻找的字符串: ")

    print("你要查找的字符串是:",string_find,'\n','1s后开始查找\n')

    time.sleep(1)
    
    os.chdir(folder_path)

    for file in glob.glob("*.docx"):

        find_string_indocx(file,string_find)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值