【python-api】python 操作word文件参考一则

脚本开发背景:

        在项目开发过QA验收的过程中,涉及到开发测试用例的工作。测试用例的文档中有一些固定格式的条目,这些条目在后续的功能演示中需要逐一解决验证情况,所以需要把这些用例提取出来。所以,花5分钟开发了一个提取word中所有以“ST-F-”开头条目的小脚本,过程中解决的主要问题是中文显示的问题有类似功能的需要开发的可以参考一下:


#coding=utf-8
import os
from docx import Document
file_list = os.listdir('.')
tag_file = ''
for file in file_list:
    if file.endswith(".doc") or file.endswith(".docx"):
        tag_file = file
if not tag_file:
    exit()
#tag_file = os.path.abspath(tag_file)
print(tag_file)
f = open(tag_file, 'rb')
document = Document(f)
l = [ paragraph.text.encode('raw_unicode_escape') for paragraph in document.paragraphs]
if "tmp.json" in file_list:
    os.remove("tmp.json")
with open("tmp.json", "w") as base_info:
    for i in l:
        if str(i.decode('unicode_escape')).startswith("ST-F-"):
            base_info.write(str(i.decode('unicode_escape'))+"\n")

这个脚本基于python2开发,py3跑不通的可以评论区告诉我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值