企业网站优化电话,职业生涯规划大赛演讲稿,贵州建设考试网站,仿淘宝网站制作centos部署的django项目#xff0c;使用libreoffice做文件转换#xff0c;官网给环境安装好libreoffice后#xff0c;可使用命令行来进行转化
还可转换其他的各种格式#xff0c;本文只做了pdf转换
import subprocess
import os def convert_to_pdf(input_file, o…centos部署的django项目使用libreoffice做文件转换官网给环境安装好libreoffice后可使用命令行来进行转化
还可转换其他的各种格式本文只做了pdf转换
import subprocess
import os def convert_to_pdf(input_file, output_file): # 检查文件扩展名 if input_file.endswith(.docx): extension -convert-to pdf elif input_file.endswith(.doc): extension -filter pdfimport -close-early else: raise ValueError(Unsupported file format) # 构建命令行参数 command flibreoffice --headless --convert-to pdf --outdir {os.path.dirname(output_file)} {input_file}{extension} # 运行命令行命令 try: subprocess.run(command, shellTrue) except subprocess.CalledProcessError as e: print(fError occurred while converting the document: {e}) return False # 检查输出文件是否存在 if not os.path.exists(output_file): raise FileNotFoundError(fFailed to create {output_file}) # 使用示例
input_file path/to/input.docx # 替换为实际的输入文件路径
output_file path/to/output.pdf # 替换为实际的输出文件路径
convert_to_pdf(input_file, output_file)