Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2013 06:11
Show Gist options
  • Save anonymous/4450319 to your computer and use it in GitHub Desktop.
Save anonymous/4450319 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#coding=cp936
import win32com
from win32com.client import Dispatch, constants
import os
import sys
def doc2pdf(input, output):
w = win32com.client.DispatchEx("Word.Application")
try:
doc = w.Documents.Open(input, ReadOnly = 1)
doc.ExportAsFixedFormat(0,output)
return 0
except:
raise
return 1
finally:
w.Quit()
def InchesToPoints( inches ):
return inches * 100/3.53
#print sys.getdefaultencoding(), sys.getfilesystemencoding()
reload(sys)
sys.setdefaultencoding('cp936')
#print sys.getdefaultencoding(), sys.getfilesystemencoding()
#w = win32com.client.Dispatch('Word.Application')
# 或者使用下面的方法,使用启动独立的进程:
w = win32com.client.DispatchEx('Word.Application')
# 后台运行,不显示,不警告
w.Visible = 0
w.DisplayAlerts = 0
# 打开新的文件
newdoc = w.Documents.Open('c:\\aa\\11.doc') # 创建新的文档
newdoc.PageSetup.TopMargin = InchesToPoints(2)
newdoc.PageSetup.BottomMargin = InchesToPoints(1)
#With Documents("Sales.doc").PageSetup
# .LeftMargin = InchesToPoints(0.75)
# .RightMargin = InchesToPoints(0.75)
# .TopMargin = InchesToPoints(1.5)
# .BottomMargin = InchesToPoints(1)
#End With
# 关闭
#newdoc.SaveAs('c:\\aa\\all.doc')
xlTypePDF = 0
newdoc.Save()
newdoc.ExportAsFixedFormat('c:\\aa\\11.pdf',17)
newdoc.Close()
w.Quit()
print '完成!~~:)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment