python word自动排版_用Python实现Word文档的自动比较

我创建了一个更通用的版本,有路径和文件检查,如果有人想要它。。。在#!/usr/bin/python3

# This uses win32com to automate the comparison of two Microsoft Word files.

# Make sure to have win32com installed for your environment and python version:

# https://github.com/mhammond/pywin32/releases

# Modified by 'pai' on basis of https://stackoverflow.com/questions/47212459/automating-comparison-of-word-documents-using-python

from os import getcwd, path

from sys import argv, exit

from win32com import client

def die(message):

print (message)

exit(1)

def cmp(original_file, modified_file):

dir = getcwd() + '\\'

print('Working...')

# some file checks

if not path.exists(dir+original_file):

die('Original file does not exist')

if not path.exists(dir+modified_file):

die('Modified file does not exist')

cmp_file = dir + original_file[:-5]+'_cmp_'+modified_file # use input filenames, but strip extension

if path.exists(cmp_file):

die('Comparison file already exists... aborting\nRemove or rename '+cmp_file)

# actual Word automation

app = client.gencache.EnsureDispatch("Word.Application")

app.CompareDocuments(app.Documents.Open(dir + original_file), app.Documents.Open(dir + modified_file))

app.ActiveDocument.ActiveWindow.View.Type = 3 # prevent that word opens itself

app.ActiveDocument.SaveAs(cmp_file)

print('Saved comparison as: '+cmp_file)

app.Quit()

def main():

if len(argv) != 3:

die('Usage: wrd_cmp ')

cmp(argv[1], argv[2])

if __name__ == '__main__':

main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值