Preface
Once at work, my friend complained that it was too troublesome to implement word to pdf in Java. I had a sudden inspiration to call python, and I just wanted to implement conversion operations in Python.
Development Environment
Java:JDK1.8
python:3.12
Code
Python code
import docx2pdf import sys import glob import os import comtypes import def w2ps(d): word_file = d pdf_file = ('.docx', '.pdf').replace('.doc', '.pdf') (word_file, pdf_file) print(f"Conversion is complete,PDFThe file has been saved as:{pdf_file}") def each(): # Get the current working directory current_directory = () # Use glob to find all .doc files doc_files = ((current_directory, '*.doc')) doc_files1 = ((current_directory, '*.docx')) # traverse the file list and print the file path if len(doc_files) > 0: # Initialize Word Application word = ('') = False # Do not display the Word interface for file_path in doc_files: doc = (file_path) # Export as PDF (file_path.replace('.doc', '.pdf'), FileFormat=17) #17 is PDF format code () () # w2ps(file_path) for file_path in doc_files1: w2ps(file_path) if __name__ == "__main__": if len() > 1: w2ps([1]) else: each() ()
Java code
import ; public class PythonCaller { public static void main111(String[] args) { String pythonScriptPath = "D:\\WorkSpace\\python\\pycorrector-master\\shany\\"; // Path to Python scripts String wordFilePath = "E:\\Create a new folder (22)\\Problem troubleshooting.docx"; // The path to the Word file to be converted try { String command = "python " + pythonScriptPath + " \"" + wordFilePath+"\""; Process process = ().exec(command); } catch (IOException e) { (); } } public static void main(String[] args) { String executablePath = "D:\\New folder\\a\\"; // Replace with the actual path of your file String wordFilePath = "E:\\Create a new folder (22)\\Problem troubleshooting.docx"; // Replace with your Word file path try { // Pass the Word file path as a command line parameter to String[] command = {executablePath, wordFilePath}; Process process = ().exec(command); // Wait for the process to complete int exitCode = (); ("Process exited with code " + exitCode); } catch (IOException | InterruptedException e) { (); } } }
Remark
Later, in order to prevent the lack of three-party dependencies, the python file was packaged separately into an exe file. An additional call to the exe file is added to the Java code.
Because the class library for doc to pdf was not found, the processing logic here is changed to if it is a doc file, Microsoft Word will be called for conversion.
When packaging py is exe, you must specify the import. The packaging command is as follows
pyinstaller --onefile --noconsole --hidden-import=
This command parsing: convert a file called exe, introduce it additionally, and does not display the command line window (--noconsole)
expand
I wrote an additional PDF to Word, and the functions and usage are basically the same.
from pdf2docx import Converter import sys import glob import os def pdf_to_word(pdf_path, word_path): cv = Converter(pdf_path) (word_path, start=0, end=None) () def p2ws(file_path): pdf_file = file_path word_file = file_path.replace('.pdf', '.docx') pdf_to_word(pdf_file, word_file) print(f"Conversion is complete,WORDThe file has been saved as:{pdf_file}") def each(): # Get the current working directory current_directory = () # Use glob to find all .doc files doc_files = ((current_directory, '*.pdf')) # traverse the file list and print the file path for file_path in doc_files: p2ws(file_path) if __name__ == "__main__": if len() > 1: p2ws([1]) else: each()
This is the article about Java calling py or exe files to implement Word to PDF. For more related Java Word to PDF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!