Python implements a method for converting doc to pdf format documents

  • 2020-07-21 08:46:53
  • OfStack

This article illustrates how Python can transform doc into pdf documents. To share for your reference, specific as follows:


#-*- 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 Document name 
    output = u'BA06007013.pdf'#pdf Document name 
  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 calls the py program


<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title> Project duplicate inspection system </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(" Please enter the project name !");
  //   CheckForm.projectname.focus();
  //   return (false);
  //  }
  //  if (document.getElementById("projectsumb").value== "" )
  //  {
  //   alert(" Please enter a project brief !");
  //   CheckForm.projectname.focus();
  //   return (false);
  //  }
 }
  </script>
</head>
<body>
<div>
<fieldset>
<legend> Project duplicate inspection system </legend>
<form name="CheckForm" method="post" action="index.php" onSubmit="return InputCheck(this)">
  <div>
  <br/>
  <label for="projectname" class="label"> The project name :</label>
  <input id="projectname" name="projectname" type="text" style="width: 400px"   class="input" />
  <divp/>
  <div>
  <br/>
  <label for="projectsumb" class="label"> Project introduction :</label>
  <textarea name="projectsumb" id="projectsumb" style="height:400px;width:800px;"></textarea>
  <div/>
  <div>
  <br/>
  <br/>
  <input type="submit" name="submit" value="  check   measuring  " class="left" />
  </div>
    <div>
  <br/>
  <label name="result" class="label"> Test results :</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"; # Note the absolute path .$name."".$sumb
  $output = exec($program)
  // $output = nl2br(shell_exec($program));
  echo mb_convert_encoding ($output,"UTF-8", "GBK");
?>

For more information about Python, please refer to Python Files and Directories, Python Coding techniques, Python Data Structures and Algorithms, Python Functions, Python String Manipulation Techniques, and Python Introductory and Advanced Classics.

I hope this article is helpful for Python programming.


Related articles: