python实现word 2007文档转换为pdf文件
作者:网海水手 时间:2021-05-27 20:57:54
在开发过程中,会遇到在命令行下将DOC文档(或者是其他Office文档)转换为PDF的要求。比如在项目中如果手册是DOC格式的,在项目发布时希望将其转换为PDF格式,并且保留DOC中的书签,链接等。将该过程整合到构建过程中就要求命令行下进行转换。
Michael Suodenjoki展示了使用Office的COM接口进行命令行下的转换。但其导出的PDF文档没有书签。在Office 2007 SP2中,微软加入了该功能,对应的接口是ExportAsFixedFormat。该方法不仅适用于Word,而且也适用于Excel。
一个简单的Python脚本来展示如何转换DOC为PDF。该脚本需要Office 2007 SP2, Python 2.6与Python for win32(使Python能调用COM)。这里也可以使用其他支持COM的语言。ExportAsFixedFormat还有其他一些参数,具体参见MSDN相关文档。需要注意的是文档路径需要为绝对路径,因为Word启动后当前路径不是调用脚本时的当前路径。
#-*- coding:utf-8 -*-
# doc2pdf.py: python script to convert doc to pdf with bookmarks!
# Requires Office 2007 SP2
# Requires python for win32 extension
import sys, os
from win32com.client import Dispatch, constants, gencache
def doc2pdf(input, output):
w = Dispatch("Word.Application")
try:
doc = w.Documents.Open(input, ReadOnly = 1)
doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,
Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks)
return 0
except:
return 1
finally:
w.Quit(constants.wdDoNotSaveChanges)
# Generate all the support we can.
def GenerateSupport():
# enable python COM support for Word 2007
# this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library"
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
def main():
if (len(sys.argv) == 2):
input = sys.argv[1]
output = os.path.splitext(input)[0]+'.pdf'
elif (len(sys.argv) == 3):
input = sys.argv[1]
output = sys.argv[2]
else:
input = u'BA06007013.docx'#word文档的名称
output = u'BA06007013.pdf'#pdf文档的名称
if (not os.path.isabs(input)):
input = os.path.abspath(input)
if (not os.path.isabs(output)):
output = os.path.abspath(output)
try:
GenerateSupport()
rc = doc2pdf(input, output)
return rc
except:
return -1
if __name__=='__main__':
rc = main()
if rc:
sys.exit(rc)
sys.exit(0)
来源:http://blog.csdn.net/rumswell/article/details/7434302
标签:python,word,pdf
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
聚族索引、非聚族索引、组合索引的含义和用途
2010-07-02 21:51:00
详解如何在Apache中运行Python WSGI应用
2021-05-16 05:24:03
白鸦:如何设计用户体验?
2008-01-03 16:53:00
js表单验证控制代码大全
2010-03-07 14:25:00
Django与AJAX实现网页动态数据显示的示例代码
2022-11-05 01:10:17
再谈CSS样式表书写风格
2009-03-30 16:09:00
![](https://img.aspxhome.com/file/UploadPic/20093/30/css_format-12s.png)
python3实现名片管理系统(控制台版)
2021-06-28 14:34:14
![](https://img.aspxhome.com/file/2023/1/101711_0s.jpg)
Python图像处理之图像的灰度线性变换
2021-12-16 22:30:58
![](https://img.aspxhome.com/file/2023/7/65977_0s.png)
用VB生成DLL封装ASP代码一个例子:连接access数据库等
2008-04-07 13:06:00
Pycharm同步远程服务器调试的方法步骤
2023-04-27 18:34:53
![](https://img.aspxhome.com/file/2023/4/102904_0s.png)
python中的文件打开与关闭操作命令介绍
2021-07-05 10:15:39
基于python指定包的安装路径方法
2023-06-04 03:10:02
python单测框架之pytest常见用法
2021-05-22 04:13:02
![](https://img.aspxhome.com/file/2023/9/69509_0s.png)
Ubuntu查看修改mysql的登录名和密码、安装phpmyadmin
2023-11-21 23:36:59
浅谈Python的条件判断语句if/else语句
2021-03-06 12:11:02
SQL Server中选出指定范围行的SQL语句写法
2008-12-09 15:04:00
基于Python实现本地音乐播放器的制作
2022-05-11 16:37:46
![](https://img.aspxhome.com/file/2023/7/80687_0s.png)
解决Python报错问题[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE]
2021-08-20 23:26:07
![](https://img.aspxhome.com/file/2023/6/105246_0s.png)
形成视觉冲击的几种方式
2008-08-03 15:57:00
![](https://img.aspxhome.com/file/UploadPic/20088/3/200883164413717s.jpg)
PHP爬虫框架盘点
2023-06-10 07:10:12
![](https://img.aspxhome.com/file/2023/8/55498_0s.jpg)