python 实现docx与doc文件的互相转换
作者:weixin_45903952 时间:2022-01-19 06:45:58
因文件格式要求,需要将docx 与doc文件相互转换,特寻找python代码,与大家共分享
from win32com import client
#转换doc为docx
def doc2docx(fn):
word = client.Dispatch("Word.Application") # 打开word应用程序
#for file in files:
doc = word.Documents.Open(fn) #打开word文件
doc.SaveAs("{}x".format(fn), 12)#另存为后缀为".docx"的文件,其中参数12或16指docx文件
doc.Close() #关闭原来word文件
word.Quit()
#转换docx为doc
def docx2doc(fn):
word = client.Dispatch("Word.Application") # 打开word应用程序
#for file in files:
doc = word.Documents.Open(fn) #打开word文件
doc.SaveAs("{}".format(fn[:-1]), 0)#另存为后缀为".docx"的文件,其中参数0指doc
doc.Close() #关闭原来word文件
word.Quit()
docx2doc(u"d:\\python\\1.docx")
如果想转换为其他格式文件,需要在format文件名内修改,并用如下save as 参数
如docx转换为pDf,用如下语句:
doc.SaveAs("{}.pdf".format(fn[:-5]), 17)
需要说明的是:
要安装OFFICE,如果是使用金山WPS的,则还不能应用
补充:python批量将文件夹内所有doc转成docx
doc转docx函数
import os
from win32com import client
def doc_to_docx(path):
if os.path.splitext(path)[1] == ".doc":
word = client.Dispatch('Word.Application')
doc = word.Documents.Open(path) # 目标路径下的文件
doc.SaveAs(os.path.splitext(path)[0]+".docx", 16) # 转化后路径下的文件
doc.Close()
word.Quit()
path = ""#填写文件夹路径
doc_to_docx(path)
获取文件夹下的所有文件的绝对路径
import os
def find_file(path, ext, file_list=[]):
dir = os.listdir(path)
for i in dir:
i = os.path.join(path, i)
if os.path.isdir(i):
find_file(i, ext, file_list)
else:
if ext == os.path.splitext(i)[1]:
file_list.append(i)
return file_list
dir_path = ""
ext = ".doc"
file_list = find_file(dir_path, ext)
源码
import os
from win32com import client
def doc_to_docx(path):
if os.path.splitext(path)[1] == ".doc":
word = client.Dispatch('Word.Application')
doc = word.Documents.Open(path) # 目标路径下的文件
doc.SaveAs(os.path.splitext(path)[0]+".docx", 16) # 转化后路径下的文件
doc.Close()
word.Quit()
def find_file(path, ext, file_list=[]):
dir = os.listdir(path)
for i in dir:
i = os.path.join(path, i)
if os.path.isdir(i):
find_file(i, ext, file_list)
else:
if ext == os.path.splitext(i)[1]:
file_list.append(i)
return file_list
dir_path = "C:\Users\python"#批量转换文件夹
ext = ".doc"
file_list = find_file(dir_path, ext)
for file in file_list:
doc_to_docx(file)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/weixin_45903952/article/details/105223595
标签:python,docx,doc
0
投稿
猜你喜欢
MySQL的6种日志详解
2008-12-18 14:36:00
Golang语言学习拿捏Go反射示例教程
2023-06-22 23:30:23
深入认识javascript中的eval函数
2008-08-03 16:44:00
利用SQL语句对不同数据库进行高效果分页
2008-11-28 14:44:00
用XMLHTTP很好的一个例子
2008-04-25 10:25:00
asp如何取回已忘记的密码?
2010-05-13 16:33:00
原生JS实现左右箭头选择日期实例代码
2023-08-06 04:55:27
Go语言实现的可读性更高的并发神库详解
2023-07-20 08:03:49
优化SQL Server的内存占用之执行缓存
2012-04-13 11:45:06
ASP图片分页代码 (通用)
2009-06-22 12:57:00
SQL学习笔记六 union联合结果集使用
2011-09-30 11:34:27
彻底弄懂CSS盒子模式之一(DIV布局快速入门)
2007-05-11 17:03:00
asp如何正确显示数据库里同时存在的GB码和BIG5编码?
2010-06-28 18:26:00
在ASP.NET 2.0中操作数据之一:创建一个数据访问层
2023-06-24 16:58:54
Python时间获取及转换知识汇总
2023-08-02 12:17:08
基于Python数据结构之递归与回溯搜索
2022-02-27 01:40:33
利用python爬取斗鱼app中照片方法实例
2022-01-03 17:40:39
python对象销毁实例(垃圾回收)
2022-07-29 06:22:14
30万条数据,搜索文本字段的各种方式对比
2010-05-02 10:17:00
教你轻松掌握MaxDB和MySQL之间的协同性
2009-02-23 17:28:00