xxxxxxxxxx
import win32com.client
adobe_app = win32com.client.Dispatch("AcroExch.App") # Launch Adobe Acrobat application
adobe_pdf = win32com.client.Dispatch("AcroExch.PDDoc") # Create new PDF document
input_word_file = "path/to/input.docx"
output_pdf_file = "path/to/output.pdf"
adobe_pdf.Open(input_word_file) # Open Word document
adobe_pdf.Save(1, output_pdf_file) # Save as PDF
adobe_pdf.Close() # Close PDF document
adobe_app.Exit() # Close Adobe Acrobat application
xxxxxxxxxx
Convert word to pdf online for free:
https://www.adobe.com/in/acrobat/online/word-to-pdf.html
xxxxxxxxxx
from docx2pdf import convert
# Specify the path of the Word document
docx_path = '/path/to/your/document.docx'
# Specify the output PDF path (optional, a default path will be used if not specified)
pdf_path = '/path/to/save/output.pdf'
# Convert Word document to PDF
convert(docx_path, pdf_path)
xxxxxxxxxx
import win32com.client as win32
def convert_word_to_pdf(input_file, output_file):
word_app = win32.Dispatch("Word.Application")
word_app.Visible = False
try:
doc = word_app.Documents.Open(input_file)
doc.SaveAs(output_file, FileFormat=17) # 17 represents PDF format
doc.Close()
finally:
word_app.Quit()
# Usage
input_file_path = "path/to/input.docx"
output_file_path = "path/to/output.pdf"
convert_word_to_pdf(input_file_path, output_file_path)
xxxxxxxxxx
from docx import Document
from PyPDF2 import PdfWriter
def convert_word_to_pdf(input_file_path, output_file_path):
# Load the Word document
doc = Document(input_file_path)
# Create a PDF writer
pdf_writer = PdfWriter()
# Iterate through each page of the Word document
for page in doc.pages:
# Extract the content from each page and add it to the PDF writer
pdf_writer.add_page(page)
# Write the PDF content to the output file
with open(output_file_path, 'wb') as output_file:
pdf_writer.write(output_file)
# Usage example
input_doc_file = 'input.docx' # Replace with the actual input Word document file path
output_pdf_file = 'output.pdf' # Replace with the desired output PDF file path
convert_word_to_pdf(input_doc_file, output_pdf_file)
xxxxxxxxxx
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class DocToPDF {
public static void main(String[] args) {
try {
InputStream templateInputStream = new FileInputStream("D:\\\\Workspace\\\\New\\\\Sample.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String outputfilepath = "D:\\\\Workspace\\\\New\\\\Sample.pdf";
FileOutputStream os = new FileOutputStream(outputfilepath);
Docx4J.toPDF(wordMLPackage,os);
os.flush();
os.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
xxxxxxxxxx
import win32com.client as win32
# Define the input Word document and output PDF file paths
input_doc = "path/to/input.docx"
output_pdf = "path/to/output.pdf"
# Create an instance of the Word Application
word_app = win32.gencache.EnsureDispatch("Word.Application")
# Open the input Word document
doc = word_app.Documents.Open(input_doc)
# Save the document as PDF
doc.SaveAs(output_pdf, FileFormat=17)
# Close the document and the Word application
doc.Close()
word_app.Quit()