批量转换word文档到pdf文件
最近在整理每周的工作记录。因为每周的工作记录大都是单独的word文件,有时候忘记了也不容易找出来,一个个打开查找太费劲,因此想着把这些文件通过word2016的另存为功能转换为pdf,然后永Acrobat合并起来。
思路如下:
(1)通过Python代码搜索指定输入目录下的所有word文档,调用word COM接口,将文件转存为pdf文件到指定输出目录;
(2)利用Acrobat将输出的目录中所有的pdf合并成单个pdf文件供存档查阅。
步骤(1)的代码如下:
import os
#import comtypes.client
import win32com.client
import time wdFormatPDF = 17 # absolute path is needed
# be careful about the slash '\', use '\\' or '/' or raw string r"..."
in_dir=u'input directory'
out_dir=u'output directory' word = win32com.client.Dispatch('Word.Application')
word.Visible = True
time.sleep(3)
first_flag=True try:
for root, dirs, files in os.walk(in_dir):
for file in files:
if (file.endswith(".docx") or file.endswith(".doc")) and (not file.startswith('~')):
in_file=os.path.join(root, file)
out_file_temp=os.path.join(out_dir,file)
out_file=out_file_temp.rsplit('.',1)[0]+u'.pdf'
# print(in_file)
# print(out_file)
# skip existed files
if os.path.isfile(out_file):
continue
print "================================================"
print "convert\n'"+in_file+"' into\n"+ out_file +"'\n"
doc=word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
if first_flag:
word.Visible = False
first_flag = False
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst) # __str__ allows args to be printed directly, word.Quit()
print "Coversion Done."
步骤(2):


可以看到每个文件的名字都变成了书签,方便查阅。
参考:
http://stackoverflow.com/questions/6011115/doc-to-pdf-using-python
批量转换word文档到pdf文件的更多相关文章
- 转换Word文档为PDF文件
1.使用 Office COM组件的Microsoft.Office.Interop.word.dll库 该方法需要在电脑上安装Office软件,并且需要Office支持转换为PDF格式,如果不支持, ...
- 微信公众号怎么添加附件?比如word文档,pdf文件等
微信公众号怎么添加附件?比如word文档,pdf文件等 我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众 ...
- JAVA使用aspose实现word文档转pdf文件
引入jar包 下载地址:https://yvioo.lanzous.com/iezpdno3mob 然后打开下载的目录打开cmd执行 mvn install:install-file -Dfile=a ...
- Java 使用 jacob 将 word 文档转换为 pdf 文件
网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...
- 使用VBA将批量的WORD文档转换为PDF
Sub BatchConvertToPDF() Dim destFolderPath As String destFolderPath = GetFolderPath If destFolderPat ...
- DEV word文档转换为pdf文件
引用aspose.net控件2.0. docement doc=new document(文件路径和名称); doc.save(输出路径\file.pdf);
- 【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式
背景: 年11月,微软宣布作为ECMA国际主要合作伙伴,将其开发的基于XML的文件格式标准化,称之为"Office Open XML" .Open XML的引进使office文档结 ...
- word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法
公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...
- 在linux中使用php将word文档转为pdf
使用本教程需要在linux中安装openoffice,改页面中有详细的安装与使用教程(http://www.cnblogs.com/sustudy/p/3999628.html). 既然,你看了该教程 ...
随机推荐
- 你所不知道的五件事情--java.util.concurrent(第二部分)
这是Ted Neward在IBM developerWorks中5 things系列文章中的一篇,仍然讲述了关于Java并发集合API的一些应用窍门,值得大家学习.(2010.06.17最后更新) 摘 ...
- [C语言 - 14] 进制
进制 二进制 int number = 0B1010; 没有二进制的输出格式 八进制 int number2 = 07613; printf("number2 = %o\ ...
- FreeModbus Slave RTU 精简版源代码【worldsing 笔记】
RTU精简版本 测试环境:IAR for avr 5.40 + M128 目前只优化了ModBusPort.c和ModBusRTU.c ModBusPort.c 566 bytes of CO ...
- ExtJs4.1实现数据缓存
Ext.onReady(function () { Ext.define("ProvinceCity", { extend: ...
- Javascript注意事项三【使用假值】
0 //NumberNaN //Number'' //Stringfalse //Booleannull //Objectundefined //Undefined这些值全部都等同于false,但是它 ...
- Cookie与Session的初探
1.Cookie 2.Session 每当一个新的请求来时,asp.net会根据浏览器有没传来SessionId(一般用Cookie传过来的,也可以用url传),来判断是新创建一个session还是根 ...
- 好的git教程
1.GitHub使用教程for VS2012 http://www.cnblogs.com/yc-755909659/p/3753355.html
- Idea的Git使用
在使用Git来pull和push代码的时候会发生冲突的情况,如果处理不当则会出现无法pull和push代码的情况.所以这个是十分有必要注意的. 那么我来说几句自己的总结吧: 1.先看下idea的Git ...
- Java条件语句 switch case
不得不说的几点小秘密: 1. switch 后面小括号中表达式的值必须是整型或字符型 2. case 后面的值可以是常量数值,如 1.2:也可以是一个常量表达式,如 2+2 :但不能是变量或带有变量的 ...
- cocos2d-x c++和object-c内存管理比较
转自:http://www.2cto.com/kf/201307/227142.html 既然选择了C++作为游戏开发的语言, 手动的管理内存是难以避免的, 而Cocos2d-x的仿Objctive- ...