当前位置: 首页 > news >正文

python文件docx转pdf

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 = f'libreoffice --headless --convert-to pdf --outdir {os.path.dirname(output_file)} {input_file}{extension}'    # 运行命令行命令    try:  subprocess.run(command, shell=True)    except subprocess.CalledProcessError as e:  print(f"Error occurred while converting the document: {e}")  return False  # 检查输出文件是否存在    if not os.path.exists(output_file):    raise FileNotFoundError(f'Failed to create {output_file}')    # 使用示例    
input_file = 'path/to/input.docx'  # 替换为实际的输入文件路径    
output_file = 'path/to/output.pdf'  # 替换为实际的输出文件路径    
convert_to_pdf(input_file, output_file)

http://www.lryc.cn/news/254360.html

相关文章:

  • 9.基于SpringBoot3+I18N实现国际化
  • 27. 深度学习进阶 - 为什么RNN
  • 谈一谈柔性数组
  • <Linux>(极简关键、省时省力)《Linux操作系统原理分析之Linux文件管理(1)》(25)
  • 算能PCIe开发环境搭建-一些记录
  • 使用C#和HtmlAgilityPack打造强大的Snapchat视频爬虫
  • c/c++的字符和字符串输入输出
  • 学习设计模式的网站
  • Hadoop学习笔记(HDP)-Part.08 部署Ambari集群
  • IDEA加载阿里Java规范插件
  • 【CSP】202305-1_重复局面Python实现
  • html5各行各业官网模板源码下载(1)
  • 6 Redis缓存设计与性能优化
  • SpringCloud常见问题
  • 实战演练 | 在 Navicat 中格式化日期和时间
  • mysql面试题分享带答案
  • 利用 Python进行数据分析实验(一)
  • Jupyter Notebook工具
  • c语言上机小练(有点难)
  • <JavaEE> 什么是线程安全?产生线程不安全的原因和处理方式
  • Kotlin 中的 also 和 run:选择正确的作用域函数
  • ZKP Understanding Nova (1): MinRoot Example
  • 0基础学java-day14
  • 创建conan包-工具链
  • IntelliJ IDE 插件开发 | (二)UI 界面与数据持久化
  • 使用vue UI安装路由插件
  • RPG项目01_脚本代码
  • 目标检测YOLO实战应用案例100讲-交通目标数据集构建及高性能检测算法研究与应用
  • 浅谈Vue.js的计算属性computed
  • Linux常用指令详解