49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import requests
|
|
import os
|
|
|
|
|
|
def convert_pdf_to_word(file_path, output_dir, output_format='docx'):
|
|
"""
|
|
Converts a PDF file to a Word document using a specified API.
|
|
|
|
:param file_path: Path to the PDF file to convert.
|
|
:param output_dir: Directory to save the converted Word document.
|
|
:param output_format: Format of the output Word document ('docx' or 'doc').
|
|
:return: None
|
|
"""
|
|
url = 'http://192.168.0.40:5000/api/v1/convert/pdf/word'
|
|
|
|
# Prepare the files and data dictionary for the multipart/form-data request
|
|
with open(file_path, 'rb') as file_handle:
|
|
files = {
|
|
'fileInput': (os.path.basename(file_path), file_handle, 'application/pdf')
|
|
}
|
|
data = {
|
|
'outputFormat': output_format
|
|
}
|
|
|
|
# Make the POST request
|
|
response = requests.post(url, files=files, data=data)
|
|
|
|
# Check the response
|
|
if response.status_code == 200:
|
|
print("Request was successful.")
|
|
|
|
# Determine the output filename based on the input filename but with the new extension
|
|
output_filename = os.path.splitext(os.path.basename(file_path))[0] + '.' + output_format
|
|
output_path = os.path.join(output_dir, output_filename)
|
|
|
|
# Save the output to a new file
|
|
with open(output_path, 'wb') as f:
|
|
f.write(response.content)
|
|
print(f"Output file saved to: {output_path}")
|
|
else:
|
|
print("Failed to make request:", response.status_code, response.text)
|
|
|
|
|
|
# Example usage:
|
|
if __name__ == '__main__':
|
|
file_path = "C:\\Users\\Administrator\\Desktop\\招标文件\\招标02_invalid.pdf"
|
|
output_dir = "C:\\Users\\Administrator\\Desktop\\招标文件"
|
|
convert_pdf_to_word(file_path, output_dir, 'doc')
|