溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Python怎么將doc格式轉(zhuǎn)化為pdf格式

發(fā)布時(shí)間:2021-02-23 15:45:27 來(lái)源:億速云 閱讀:157 作者:戴恩恩 欄目:開(kāi)發(fā)技術(shù)

本文章向大家介紹使用Python怎么將doc格式轉(zhuǎn)化為pdf格式,主要包括使用Python怎么將doc格式轉(zhuǎn)化為pdf格式的使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。

Python主要用來(lái)做什么

Python主要應(yīng)用于:1、Web開(kāi)發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲(chóng);4、嵌入式應(yīng)用開(kāi)發(fā);5、游戲開(kāi)發(fā);6、桌面應(yīng)用開(kāi)發(fā)。

具體如下:

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"    class="input" />
  <divp/>
  <div>
  <br/>
  <label for="projectsumb" class="label">項(xiàng)目簡(jiǎn)介:</label>
  <textarea name="projectsumb" id="projectsumb" ></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");
  // $path2="../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");
?>

到此這篇關(guān)于使用Python怎么將doc格式轉(zhuǎn)化為pdf格式的文章就介紹到這了,更多相關(guān)的內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI