doc.ExportAsFixedFormat 值不在预期的范围内范围内

&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
Python 将word文件转换为PDF文件
摘要:原文地址http://blog.csdn.net/rumswell/article/details/7434302#-*-coding:utf-8-*-#doc2pdf.py:pythonscripttoconvertdoctopdfwithbookmarks!#RequiresOffice2007SP2#Requirespythonforwin32extension'''参考:http://blog.csdn.net/rumswell/article/details/743
原文地址http://blog.csdn.net/rumswell/article/details/7434302 #-*- coding:utf-8 -*-# doc2pdf.py: python script to convert doc to pdf with bookmarks!# Requires Office 2007 SP2# Requires python for win32 extension'''参考:http://blog.csdn.net/rumswell/article/details/7434302'''import sys, osfrom win32com.client import Dispatch, constants, gencachefrom config import REPORT_DOC_PATH,REPORT_PDF_PATHdef word2pdf(filename): input=filename+'.docx' output=filename+'.pdf' pdf_name=output #判断文件是否存在 os.chdir(REPORT_DOC_PATH) if not os.path.isfile(input): print u'%s not exist'%input return False #文档路径需要为绝对路径,因为Word启动后当前路径不是调用脚本时的当前路径。 if (not os.path.isabs(input)):#判断是否为绝对路径 #os.chdir(REPORT_DOC_PATH) input = os.path.abspath(input)#返回绝对路径 else: print u'%s not absolute path'%input return False if (not os.path.isabs(output)): os.chdir(REPORT_PDF_PATH) output = os.path.abspath(output) else: print u'%s not absolute path'%output return False try: print input,output # enable python COM support for Word 2007 # this is generated by: makepy.py -i &Microsoft Word 12.0 Object Library& gencache.EnsureModule('{0-}', 0, 8, 4) #开始转换 w = Dispatch(&Word.Application&) try: doc = w.Documents.Open(input, ReadOnly = 1) doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,/ Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks) except: print ' exception' finally: w.Quit(constants.wdDoNotSaveChanges) if os.path.isfile(pdf_name): print 'translate success' return True else: print 'translate fail' return False except: print ' exception' return -1if __name__=='__main__': rc = word2pdf('1') if rc: sys.exit(rc) sys.exit(0)
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
为您提供0门槛上云实践机会
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
Python 将word文件转换为PDF文件相关信息,包括
的信息,所有Python 将word文件转换为PDF文件相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International02-1602-1602-1602-1602-1602-1602-1602-1602-1602-16最新范文01-0101-0101-0101-0101-0101-0101-0101-0101-0101-0101-0101-0101-0101-0101-01&|&&|&&|&&|&&
当前位置: >
word ppt excel文档转换成pdf
作者:互联网 & 来源:转载 &
摘要: 1.把word文档转换成pdf
(1).添加引用
1 using Microsoft.Office.Interop.W
(2).转换方法
1 /// &summary&
/// 把Word文件转换成pdf文件
/// &/summary&
/// &param name=&quo
1.把word文档转换成pdf
(1).添加引用
1 using Microsoft.Office.Interop.W
(2).转换方法
1 /// &summary&
/// 把Word文件转换成pdf文件
/// &/summary&
/// &param name=&sourcePath&&需要转换的文件路径和文件名称&/param&
/// &param name=&targetPath&&转换完成后的文件的路径和文件名名称&/param&
/// &returns&成功返回true,失败返回false&/returns&
public static bool WordToPdf(string sourcePath, string targetPath)
bool result = false; 10
WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式
object missing = Type.M 12
Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null; 13
Document document = null; 14
applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass(); 17
object inputfileName = sourceP//需要转格式的文件路径
string outputFileName = targetP//转换完成后PDF或XPS文件的路径和文件名名称
WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式
bool openAfterExport = false;//转换完成后是否打开
WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForP//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。
WdExportRange wdExportAllDocument = WdExportRange.wdExportAllD//导出全部内容(枚举)
int from = 0;//起始页码
int to = 0;//结束页码
WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentC//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记
bool includeDocProps = true;//指定是否包含新导出的文件在文档属性
bool keepIRM = true;// 28
WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordB//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。
bool docStructureTags = true; 30
bool bitmapMissingFonts = true; 31
bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)
document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 33
if (document != null) 34
document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing); 36
result = true; 38
result = false; 42
if (document != null) 46
document.Close(ref missing, ref missing, ref missing); 48
document = null; 49
if (applicationClass != null) 51
applicationClass.Quit(ref missing, ref missing, ref missing); 53
applicationClass = null; 54
/// &summary&
/// 把Word文件转换成pdf文件
/// &/summary&
/// &param name=&sourcePath&&需要转换的文件路径和文件名称&/param&
/// &param name=&targetPath&&转换完成后的文件的路径和文件名名称&/param&
/// &returns&成功返回true,失败返回false&/returns&
public static bool WordToPdf(object sourcePath, string targetPath)
bool result = false;
WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
object missing = Type.M
Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
Document document = null;
applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (document != null)
document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
result = true;
result = false;
if (document != null)
document.Close(ref missing, ref missing, ref missing);
document = null;
if (applicationClass != null)
applicationClass.Quit(ref missing, ref missing, ref missing);
applicationClass = null;
1 OfficeToPdf.WordToPdf(&d:\\1234.doc&, &d:\\1234.pdf&);
2.把Excel文档转换成pdf
(1).添加引用
1 using Microsoft.Office.Interop.E
(2).转换方法
/// &summary&
/// 把Excel文件转换成pdf文件
/// &/summary&
/// &param name=&sourcePath&&需要转换的文件路径和文件名称&/param&
/// &param name=&targetPath&&转换完成后的文件的路径和文件名名称&/param&
/// &returns&&/returns&
public static bool ExcelToPdf(string sourcePath, string targetPath)
bool result = false; 10
XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
object missing = Type.M 12
Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null; 13
Workbook workbook = null; 14
applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass(); 17
string inputfileName = sourceP//需要转格式的文件路径
string outputFileName = targetP//转换完成后PDF文件的路径和文件名名称
XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityS//1.xlQualityStandard:质量标准,2.xlQualityM最低质量
bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
bool openAfterPublish = false;//发布后不打开
workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); 24
if (workbook!=null) 25
workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing); 27
result = true; 29
result = false; 33
if (workbook != null) 37
workbook.Close(true, missing, missing); 39
workbook = null; 40
if (applicationClass != null) 42
applicationClass.Quit(); 44
applicationClass = null; 45
/// &summary&
/// 把Excel文件转换成pdf文件
/// &/summary&
/// &param name=&sourcePath&&需要转换的文件路径和文件名称&/param&
/// &param name=&targetPath&&转换完成后的文件的路径和文件名名称&/param&
/// &returns&&/returns&
public static bool ExcelToPdf(string sourcePath, string targetPath)
bool result = false; 10
XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
object missing = Type.M 12
Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null; 13
Workbook workbook = null; 14
applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass(); 17
workbook = applicationClass.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); 18
if (workbook != null) 19
workbook.ExportAsFixedFormat(xlTypePDF, targetPath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); 21
result = true; 23
result = false; 27
if (workbook != null) 31
workbook.Close(true, missing, missing); 33
workbook = null; 34
if (applicationClass != null) 36
applicationClass.Quit(); 38
applicationClass = null; 39
1 OfficeToPdf.ExcelToPdf(&d:\\1234.xls&, &d:\\1234.pdf&);
3.把ppt转换成pdf
(1).添加引用
1 using Microsoft.Office.C
2 using Microsoft.Office.Interop.PowerP
(2).转换方法
///&summary&
/// 把PowerPoint文件转换成PDF格式文件
///&/summary&
///&param name=&sourcePath&&源文件路径&/param&
///&param name=&targetPath&&目标文件路径&/param&
///&returns&成功返回true,失败返回false&/returns&
public static bool PPTConvertToPDF(string sourcePath, string targetPath)
PpSaveAsFileType ppSaveAsFileType = PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
object missing = Type.M
Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
Presentation persentation = null;
application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
if (persentation!=null)
persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
result = true;
result = false;
if (persentation != null)
persentation.Close();
persentation = null;
if (application != null)
application.Quit();
application = null;
1 OfficeToPdf.PPTToPDF(&d:\\12345.pptx&, &d:\\12345.pdf&);
版权所有 IT知识库 CopyRight (C)
IT知识库 IT610.com , All Rights Reserved.

我要回帖

更多关于 c 值不在预期范围内 的文章

 

随机推荐