网站很久没被收录的新闻怎么处理东西湖区城乡建设局网站
web/
2025/9/27 3:56:11/
文章来源:
网站很久没被收录的新闻怎么处理,东西湖区城乡建设局网站,wordpress好学吗,重庆好网互联前言
最近在搞知识库#xff0c;需要把各种 Word、PowerPoint、Excel 文件转换成 PDF 文件#xff0c;不然 Word 中的表格中的文字提取会出现一些问题#xff1b;使用 Office 或者 WPS 将大量文件转换成 PDF 需要频繁重复打开文件#xff0c;点击保存为PDF#xff0c;然后…前言
最近在搞知识库需要把各种 Word、PowerPoint、Excel 文件转换成 PDF 文件不然 Word 中的表格中的文字提取会出现一些问题使用 Office 或者 WPS 将大量文件转换成 PDF 需要频繁重复打开文件点击保存为PDF然后再关闭非常繁琐。所以就简单的写了下面的 Python 程序来帮助我转化。
代码
下面的程序需要使用开源 Office 工具 —— libreoffice安装该工具后才能使用下面的命令行才能使用 libreoffice 中的 PDF 转化功能将文档转化。
import os
import subprocess
from pathlib import Path
from tkinter import Tk, filedialog, messageboxdef convert_to_pdf(input_file):try:# Get the directory of the input fileinput_dir os.path.dirname(input_file)# Prepare output file pathoutput_file os.path.join(input_dir, Path(input_file).stem .pdf)# Convert to PDF using LibreOfficesubprocess.run([soffice, --headless, --convert-to, pdf, input_file, --outdir, input_dir],checkTrue)print(fConverted {input_file} to {output_file})return output_fileexcept Exception as e:print(fError converting file {input_file}: {e})return Nonedef select_files_and_convert():# Supported file extensionssupported_extensions [ (files, *.doc;*.docx;*.ppt;*.pptx;*.xls;*.xlsx),(Word files, *.doc;*.docx),(PowerPoint files, *.ppt;*.pptx),(Excel files, *.xls;*.xlsx)]# Create Tkinter root windowroot Tk()root.withdraw() # Hide the root window# Open file dialog to select filesfiles filedialog.askopenfilenames(titleSelect files to convert, filetypessupported_extensions)if not files:messagebox.showinfo(No files selected, No files were selected for conversion.)return# Convert selected filesfor file in files:output_file convert_to_pdf(file)if output_file:# messagebox.showinfo(Success, fConverted {file} to {output_file})print(fConverted {file} to {output_file}) else:# messagebox.showerror(Error, fFailed to convert {file})print(fFailed to convert {file})def main():select_files_and_convert()if __name__ __main__:main()
效果展示
效果大致如下点击运行后就会弹出选择框批量选择文件后就会开始转化。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82522.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!