python实现word文档多内容替换

直接上代码:

利用docx读取.docx格式

利用win32com模块对.doc文档转为docx

 

脚本调用: python your.py your.doc/your.docx 被替换字符A/被替换字符B/被替换字符C 替换字符a/替换字符b/替换字符c

from win32com import client as wc
import os
import docx
import sys

def help():
    print("Format: python py_file path+filename.doc/.docx A/B/C a/b/c\n"
          "eg: python preplace_string.py E:/....../a.docx 你好/好的/谢谢 你好啊/嗯嗯/不客气\n"
          "eg: python preplace_string.py E:/....../a.doc 你好/好的/谢谢 你好啊/嗯嗯/不客气\n"
		  "EXE:\n"
		  "eg: preplace_string.exe E:/....../a.doc 你好/好的/谢谢 你好啊/嗯嗯/不客气\n"
		  "eg: preplace_string.exe E:/....../a.docx 你好/好的/谢谢 你好啊/嗯嗯/不客气\n"
          "替换字符串与被替换字符串一一对应,不同字符串用/分割\n")
if __name__=='__main__':
    #print(sys.argv)
    num=len(sys.argv)

    if(num!=4):
        help()
        sys.exit(1)

    path=sys.argv[1]#file
    if not os.path.exists(path):
        print(path,'does not exist!!!!!\n')
    path=os.path.abspath(path)
    basename,ext=os.path.splitext(path)
    if ext=='.doc':
        try:
            #webbrowser.open(path)
            word = wc.Dispatch("Word.Application")
            doc = word.Documents.Open(path)
            doc.SaveAs(basename+'.docx', 12)# 12为docx
            doc.Close()
            word.Quit()
        except:
            print("can not read "+path+"!\nPlease check out that whether the format of the file is doc/docx Or Does this file exist!!!")
            sys.exit(1)
    elif ext!='.docx':
        print("can not read" + path + "!\nPlease check out that whether the format of the file is doc/docx!!!")
        sys.exit(1)
    old_string=sys.argv[2]#old_string="你好|好的|谢谢"
    new_string=sys.argv[3]#new_string="你好啊|嗯嗯|不客气"
    olds=old_string.split('/')
    news=new_string.split('/')
    if(len(olds)!=len(news)):
        print('The number of replacement strings does not match the number of replaced strings,Please Check!')
        print('Old string:',olds)
        print('New string:',news)
        sys.exit(1)

 
    path=basename + '.docx'
    file =docx.Document(path)

    for i in range(len(olds)):
        for paragraph in file.paragraphs:
            paragraph.text = paragraph.text.replace(olds[i],news[i])
    #file.save('test'+'_temp.docx')
    file.save(basename + '_temp.docx')
    print('succes!\n'
          'new file is shown in ' + basename+'_temp.docx')

 

### 回答1: 使用python的docx库可以实现word文档文件内容转换为字符串,具体可以使用如下代码:``` from docx import Documentdocument = Document('document.docx')text = ""for para in document.paragraphs: text += para.text ``` ### 回答2: 在Python中可以使用python-docx库来实现Word文档文件内容转换为字符串。 首先,需要安装python-docx库,可以使用pip命令来进行安装,如下所示: ```python pip install python-docx ``` 安装完成后,可以使用以下代码来实现Word文档内容转换为字符串: ```python from docx import Document # 打开Word文档 doc = Document('example.docx') # 创建一个空字符串用于存储文本内容 text = '' # 遍历文档的每一段落 for para in doc.paragraphs: text += para.text # 输出转换后的字符串 print(text) ``` 在这段代码中,首先使用Document类打开Word文档,然后遍历文档的每一段落,将每个段落的文本内容添加到一个空字符串中。最后,我们可以使用print语句将转换后的字符串输出。 请注意,上述代码仅适用于提取文本内容,对于包含表格、图片等复杂结构的Word文档可能无法正确提取。如果需要处理更复杂的Word文档,请查阅python-docx库的官方文档以获取更多用法和示例。 ### 回答3: 在Python中,可以使用python-docx库来实现Word文档文件内容转换为字符串。 首先,需要确保已经在系统中安装了python-docx库。如果没有安装,可以使用以下命令进行安装: ``` pip install python-docx ``` 接下来,可以按照以下步骤使用python-docx库将Word文档文件内容转换为字符串: 1. 导入python-docx库: ```python from docx import Document ``` 2. 打开Word文档文件: ```python doc = Document('path/to/word/document.docx') ``` 将`path/to/word/document.docx`替换为实际的文件路径。 3. 遍历文档的所有段落,并将内容追加到字符串中: ```python text = '' for paragraph in doc.paragraphs: text += paragraph.text + ' ' ``` 4. 打印转换后的字符串内容: ```python print(text) ``` 完整的代码示例: ```python from docx import Document doc = Document('path/to/word/document.docx') text = '' for paragraph in doc.paragraphs: text += paragraph.text + ' ' print(text) ``` 以上代码将会将指定的Word文档文件的内容转换为字符串,并打印输出。请注意替换`path/to/word/document.docx`为实际文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值