Convert Word documents to PDF files in bulk

Source: Internet
Author: User

I've been working on a weekly record. Because the weekly work records are mostly separate word files, sometimes forget it is not easy to find out, one by one to open the search too hard, so think of these files through the word2016 of the Save as function into PDF, and then Yong Acrobat merged together.

Ideas are as follows:

(1) Search through the Python code for all Word documents in the specified input directory, call the Word COM interface, and dump the file into a PDF file to the specified output directory;

(2) Use Acrobat to merge all the PDFs in the output directory into a single PDF file for archival review.

The code for Step (1) is as follows:

1 ImportOS2 #Import Comtypes.client3 Importwin32com.client4 Import Time5 6 7Wdformatpdf = 178 9 #absolute path is neededTen #Be careful about the slash ' \ ', use ' \ \ ' or '/' or raw string R ' ... " OneIn_dir=u'Input Directory' AOut_dir=u'Output Directory' -  -Word = Win32com.client.Dispatch ('Word.Application') theWord. Visible =True -Time.sleep (3) -first_flag=True -  + Try: -      forRoot, dirs, filesinchOs.walk (in_dir): +          forFileinchFiles: A             if(File.endswith (". docx")orFile.endswith (". doc")) and( notFile.startswith ('~')): atin_file=os.path.join (root, file) -out_file_temp=Os.path.join (out_dir,file) -Out_file=out_file_temp.rsplit ('.', 1) [0]+u'. pdf' -     #print (in_file) -     #print (out_file) -                 #Skip existed files in                 ifOs.path.isfile (out_file): -                     Continue to                 Print "================================================" +                 Print "convert\n '"+in_file+"' into\n"+ Out_file +"' \ n"  -Doc=Word. Documents.Open (In_file) theDoc. SaveAs (Out_file, fileformat=wdformatpdf) * Doc. Close () $                 ifFirst_flag:Panax NotoginsengWord. Visible =False -First_flag =False the exceptException as Inst: +     Print(Type (inst))#The exception instance A     Print(Inst.args)#arguments stored in. Args the     Print(Inst)#__str__ allows args to be printed directly, +                - Word. Quit () $ Print "coversion done."

Step (2):

You can see the name of each file becomes a bookmark for easy reference.

Reference:

Http://stackoverflow.com/questions/6011115/doc-to-pdf-using-python

Convert Word documents to PDF files in bulk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.