[PYTHON] Beginners try to convert Word files to PDF at once

Trigger for the initiative

I often use Word to create documents, but After promoting paperless offices and introducing groupware, Start with a voice from your boss saying, "Let's convert Word files to PDF and manage documents centrally !!"

The battle with countless ".doc" and ".docx" that I do not know how many begins ... Of course, no one in the company can use python, so I tried it implicitly.


import win32com.client
import os
#Install the required modules

wd = win32com.client.Dispatch('Word.Application')
#Declaration to "manipulate Word"

documents_path = 'C:/test/'
word_list = []
#Set the path of the folder where the files are stored
#word_file_Empty list to store name(word_list)Create

for word_file_name in os.listdir(documents_path):
    if os.path.isfile(os.path.join(documents_path, word_file_name)):

for i in range(0,len(word_list)):
    word_file = wd.Documents.Open(documents_path + '/' + word_list[i])
    base_name, ext = os.path.splitext(word_list[i])
    #base_name,By ext, the extension is entered in ext and the file name is base._Stored in name
    word_file.SaveAs2(FileName = documents_path + base_name + '.pdf', FileFormat = 17)
    #Specify file name, format(17 is PDF[https://docs.microsoft.com/ja-JP/office/vba/api/word.wdsaveformat])The set
  except Exception as e:
    #Store exception handling other than try in e, and display if there is


After a lot of trial and error, I got the above code. I think it's quite redundant because it's a beginner.

documents_path is for posting this time, so it is set as test, When using it, specify the path of the folder where the Word file is stored.


・ The for statement is included twice, and I originally wanted to combine it into one. In the with statement, pywin32 did not work well, so it is necessary to take improvement measures in the future.

-Try with modules other than pywin32.

・ Clean up redundant parts.

Try making your own for the first time

I was originally interested in python, and I've been working on it, but it was the first time I actually completed something. It may be a bad idea for professionals, but I would like to get used to writing even the smallest code. Since there are many parts that can be improved in-house by using automation, we will steadily post the results of our efforts to Qiita as a memorandum.

We would appreciate any improvements or advice! Thank you.

Recommended Posts

Beginners try to convert Word files to PDF at once
[Selenium/Python] Try to display the court case pdf at once
Convert memo at once with Python 2to3
Convert PDF files to PNG files with GIMP
Convert multiple jpg files to one PDF file
Batch convert PSD files in directory to PDF
Convert multiple proto files at once with python
Rsync multiple files at once
Batch convert image files uploaded to MS Forms / Google Forms to PDF
Convert PDF to Documents by OCR
Convert markdown to PDF in Python
Convert A4 PDF to A3 every 2 pages
Convert files written in python etc. to pdf with syntax highlighting
Convert from pdf to txt 2 [pyocr]
Convert a large number of PDF files to text files using pdfminer
Convert PDF to image with ImageMagick
[To Twitter gentlemen] I wrote a script to convert .jpg-large to .jpg at once.
Convert from PDF to CSV with pdfplumber
Convert PDF attached to email to text format
Convert HEIC files to PNG files with Python
Set Expire to Redis key at once
One-liner to create a large number of test files at once on Linux
Create an API to convert PDF files to TIF images with FastAPI and Docker
Convert images in multiple folders to different pdfs for each folder at once
How to create large files at high speed
How to save all Instagram photos at once
Convert FBX files to ASCII <-> BINARY in Python
Convert PDF to image (JPEG / PNG) with Python
Erase image files at once with one liner
AI beginners try to make professional student bots
Convert UTF-8 CSV files to read in Excel
How to convert SVG to PDF and PNG [Python]
Command to automatically update pip library at once
Try to calculate RPN in Python (for beginners)
[Small story] Easy way to convert Jupyter to PDF
Register multiple self-made styles in Word at once