Python實(shí)現(xiàn)將doc轉(zhuǎn)化pdf格式文檔的方法
本文實(shí)例講述了Python實(shí)現(xiàn)將doc轉(zhuǎn)化pdf格式文檔的方法。分享給大家供大家參考,具體如下:
#-*- 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():
print(len(sys.argv))
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__':
print("hello")
rc = main()
if rc:
sys.exit(rc)
sys.exit(0)
php調(diào)用py程序
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>項(xiàng)目查重檢測(cè)系統(tǒng)</title>
<style type="text/css">
html{font-size:16px;}
fieldset{width:1080px; margin: 0 auto;}
legend{font-weight:bold; font-size:14px;}
label{float:left; width:120px; margin-left:10px;}
.left{margin-left:120px;}
.input{width:150px;}
span{color: #666666;}
</style>
<script language=JavaScript>
<!--
// function InputCheck(CheckForm)
// {
// if (CheckForm.projectname.value == "" )
// {
// alert("請(qǐng)輸入項(xiàng)目名稱!");
// CheckForm.projectname.focus();
// return (false);
// }
// if (document.getElementById("projectsumb").value== "" )
// {
// alert("請(qǐng)輸入項(xiàng)目簡(jiǎn)介!");
// CheckForm.projectname.focus();
// return (false);
// }
}
</script>
</head>
<body>
<div>
<fieldset>
<legend>項(xiàng)目查重檢測(cè)系統(tǒng)</legend>
<form name="CheckForm" method="post" action="index.php" onSubmit="return InputCheck(this)">
<div>
<br/>
<label for="projectname" class="label">項(xiàng)目名稱:</label>
<input id="projectname" name="projectname" type="text" style="width: 400px" class="input" />
<divp/>
<div>
<br/>
<label for="projectsumb" class="label">項(xiàng)目簡(jiǎn)介:</label>
<textarea name="projectsumb" id="projectsumb" style="height:400px;width:800px;"></textarea>
<div/>
<div>
<br/>
<br/>
<input type="submit" name="submit" value=" 檢 測(cè) " class="left" />
</div>
<div>
<br/>
<label name="result" class="label">檢測(cè)結(jié)果:</label>
<label name="outresult" class="label"></label>
<br/>
<div/>
</form>
<br/>
<br/>
</div>
</body>
</html>
<?php
$name=mb_convert_encoding($_POST['projectname'], "GBK","UTF-8");
// $sumb=mb_convert_encoding($_POST['projectsumb'], "GBK","UTF-8");
// $path1="../docTopdf/commFile/test.doc";
$program="D:/Users/Administrator/Anaconda3/python ../docTopdf/DocToPdf/test1.py"; #注意使用絕對(duì)路徑.$name."".$sumb
$output = exec($program)
// $output = nl2br(shell_exec($program));
echo mb_convert_encoding ($output,"UTF-8", "GBK");
?>
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python 如何優(yōu)雅的將數(shù)字轉(zhuǎn)化為時(shí)間格式的方法
- Python使用ffmpy將amr格式的音頻轉(zhuǎn)化為mp3格式的例子
- python將秒數(shù)轉(zhuǎn)化為時(shí)間格式的實(shí)例
- python實(shí)現(xiàn)將excel文件轉(zhuǎn)化成CSV格式
- Python實(shí)現(xiàn)小數(shù)轉(zhuǎn)化為百分?jǐn)?shù)的格式化輸出方法示例
- windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
- python 時(shí)間戳與格式化時(shí)間的轉(zhuǎn)化實(shí)現(xiàn)代碼
- python webp圖片格式轉(zhuǎn)化的方法
相關(guān)文章
Python faker生成器生成虛擬數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了Python faker生成器生成虛擬數(shù)據(jù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
python進(jìn)程結(jié)束后端口占用問(wèn)題解析
這篇文章主要為大家介紹了python中在進(jìn)程結(jié)束后端口依然被占用的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-01-01
Python機(jī)器學(xué)習(xí)庫(kù)sklearn(scikit-learn)的基礎(chǔ)知識(shí)和高級(jí)用法
Scikit-Learn是 Python 最流行的機(jī)器學(xué)習(xí)庫(kù)之一,它提供了各種工具來(lái)實(shí)現(xiàn)、評(píng)估和探索各種學(xué)習(xí)算法,用于,各種機(jī)器學(xué)習(xí)任務(wù),在本教程中,我們將介紹 Scikit-Learn 的基礎(chǔ)知識(shí)和一些高級(jí)用法,并提供一些實(shí)例代碼來(lái)幫助我們更好地理解2023-07-07
Python運(yùn)行錯(cuò)誤異常代碼含義對(duì)照表
這篇文章主要介紹了Python運(yùn)行錯(cuò)誤異常代碼含義對(duì)照表,需要的朋友可以參考下2021-04-04

