介绍
怎么在python中多克斯将与医生文件进行转换?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
得到win32com import 客户端 #转换医生为多克斯 def doc2docx (fn): 时间=word 才能;client.Dispatch (“Word.Application"), #,打开词应用程序 #才能for file 拷贝文件: doc 才能=,word.Documents.Open (fn), #打开词文件 doc.SaveAs才能(“{}x" .format (fn), 12) #另存为后缀为“.docx"的文件,其中参数12或16指多克斯文件 doc.Close才能(),#关闭原来词文件 word.Quit才能() #转换多克斯为医生 def docx2doc (fn): 时间=word 才能;client.Dispatch (“Word.Application"), #,打开词应用程序 #才能for file 拷贝文件: doc 才能=,word.Documents.Open (fn), #打开词文件 doc.SaveAs才能(“{}“.format (fn(: 1)), 0) #另存为后缀为“.docx"的文件,其中参数0指医生 doc.Close才能(),#关闭原来词文件 word.Quit才能() python docx2doc (u" d: \ \ \ \ 1. docx")
如果想转换为其他格式文件,需要在格式文件名内修改,并用如下另存为参数
如多克斯转换为pDf,用如下语句:
doc.SaveAs (“{} .pdf" .format (fn [5]), 17)
需要说明的是:
要安装办公室,如果是使用金山WPS的,则还不能应用
<强>补充:python批量将文件夹内所有医生转成多克斯强>
多克斯医生转函数
import 操作系统 得到win32com import 客户端 , def doc_to_docx(路径): if 才能;os.path.splitext(路径)[1],==,“.doc": ,,,word =, client.Dispatch (& # 39; Word.Application& # 39;) ,,,doc =, word.Documents.Open(路径),#,目标路径下的文件 ,,,doc.SaveAs (os.path.splitext(路径)[0]+“.docx",, 16), #,转化后路径下的文件 ,,,doc.Close () ,,,word.Quit () , 时间=path ““#填写文件夹路径 doc_to_docx(路径)
获取文件夹下的所有文件的绝对路径
import os def find_file(路径,ext, file_list=[]): 时间=dir 才能;os.listdir(路径) for 才能小姐:拷贝dir: ,,,小姐:=,os.path.join(路径,,我) ,,,if os.path.isdir(我): ,,,,,find_file (ext,我还以为;file_list) ,,,: ,,,,,if ext ==, os.path.splitext(我)[1]: ,,,,,,,file_list.append(我) return 才能;file_list , dir_path =,““ 时间=ext “.doc" 时间=file_list find_file (dir_path, ext)
源码
import 操作系统 得到win32com import 客户端 , def doc_to_docx(路径): if 才能;os.path.splitext(路径)[1],==,“.doc": ,,,word =, client.Dispatch (& # 39; Word.Application& # 39;) ,,,doc =, word.Documents.Open(路径),#,目标路径下的文件 ,,,doc.SaveAs (os.path.splitext(路径)[0]+“.docx",, 16), #,转化后路径下的文件 ,,,doc.Close () ,,,word.Quit () , def find_file(路径,ext, file_list=[]): 时间=dir 才能;os.listdir(路径) for 才能小姐:拷贝dir: ,,,小姐:=,os.path.join(路径,,我) ,,,if os.path.isdir(我): ,,,,,find_file (ext,我还以为;file_list) ,,,: ,,,,,if ext ==, os.path.splitext(我)[1]: ,,,,,,,file_list.append(我) return 才能;file_list , 时间=dir_path “C: \ \用户python" #批量转换文件夹 时间=ext “.doc" 时间=file_list find_file (dir_path, ext) for file  file_list拷贝: doc_to_docx才能(文件)
关于怎么在python中多克斯将与医生文件进行转换问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。