import os
import sys
import pandas as pd
from PIL import Image
import PyPDF2
from PyPDF2 import PdfReader # 新增导入
import fitz # pymupdf
from pdf2docx import Converter
from pdf2image import convert_from_path
import camelot.io as camelot
from openpyxl import Workbook
from win32com.client import DispatchEx
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from docx import Document # 新增导入
class OfficeConverterApp:
def __init__(self, root):
self.root = root
self.root.title("办公文档格式转换工具")
self.root.geometry("800×600")
self.root.resizable(True, True)
# 创建主标签页
self.tab_control = ttk.Notebook(root)
# 创建各个功能标签页
self.tab_csv2excel = ttk.Frame(self.tab_control)
self.tab_word2html = ttk.Frame(self.tab_control)
self.tab_word2pdf = ttk.Frame(self.tab_control)
self.tab_img2pdf = ttk.Frame(self.tab_control)
self.tab_pdf2docx = ttk.Frame(self.tab_control)
self.tab_pdf2img = ttk.Frame(self.tab_control)
self.tab_pdf2txt = ttk.Frame(self.tab_control)
self.tab_pdf2excel = ttk.Frame(self.tab_control)
self.tab_ppt2pdf = ttk.Frame(self.tab_control)
self.tab_excel2csv = ttk.Frame(self.tab_control)
self.tab_excel2pdf = ttk.Frame(self.tab_control)
# 添加标签页
self.tab_control.add(self.tab_csv2excel, text="CSV转Excel")
self.tab_control.add(self.tab_word2html, text="Word转HTML")
self.tab_control.add(self.tab_word2pdf, text="Word转PDF")
self.tab_control.add(self.tab_img2pdf, text="图片转PDF")
self.tab_control.add(self.tab_pdf2docx, text="PDF转Word")
self.tab_control.add(self.tab_pdf2img, text="PDF转图片")
self.tab_control.add(self.tab_pdf2txt, text="PDF转文本")
self.tab_control.add(self.tab_pdf2excel, text="PDF转Excel")
self.tab_control.add(self.tab_ppt2pdf, text="PPT转PDF")
self.tab_control.add(self.tab_excel2csv, text="Excel转CSV")
self.tab_control.add(self.tab_excel2pdf, text="Excel转PDF")
self.tab_control.pack(expand=1, fill="both")
# 初始化所有标签页
self.init_csv2excel_tab()
self.init_word2html_tab()
self.init_word2pdf_tab()
self.init_img2pdf_tab()
self.init_pdf2docx_tab()
self.init_pdf2img_tab()
self.init_pdf2txt_tab()
self.init_pdf2excel_tab()
self.init_ppt2pdf_tab()
self.init_excel2csv_tab()
self.init_excel2pdf_tab()
# 1. CSV转Excel
def init_csv2excel_tab(self):
frame = ttk.Frame(self.tab_csv2excel, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入CSV文件:").grid(row=0, column=0, sticky="w", pady=5)
self.csv_input_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.csv_input_path, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_csv_input).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出Excel文件:").grid(row=1, column=0, sticky="w", pady=5)
self.excel_output_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.excel_output_path, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_excel_output).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.csv_to_excel).grid(row=2, column=1, pady=20)
def browse_csv_input(self):
path = filedialog.askopenfilename(filetypes=[("CSV文件", "*.csv")])
if path:
self.csv_input_path.set(path)
def browse_excel_output(self):
path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel文件", "*.xlsx")])
if path:
self.excel_output_path.set(path)
def csv_to_excel(self):
input_path = self.csv_input_path.get()
output_path = self.excel_output_path.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
data = pd.read_csv(input_path, index_col=0)
data.to_excel(output_path)
messagebox.showinfo("成功", f"转换成功!文件已保存至:{output_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 2. Word转HTML
def init_word2html_tab(self):
frame = ttk.Frame(self.tab_word2html, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入Word文件:").grid(row=0, column=0, sticky="w", pady=5)
self.word_input_path_html = tk.StringVar()
ttk.Entry(frame, textvariable=self.word_input_path_html, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_word_input_html).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出HTML文件:").grid(row=1, column=0, sticky="w", pady=5)
self.html_output_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.html_output_path, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_html_output).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.word_to_html).grid(row=2, column=1, pady=20)
def browse_word_input_html(self):
path = filedialog.askopenfilename(filetypes=[("Word文件", "*.doc;*.docx")])
if path:
self.word_input_path_html.set(path)
def browse_html_output(self):
path = filedialog.asksaveasfilename(defaultextension=".html", filetypes=[("HTML文件", "*.html")])
if path:
self.html_output_path.set(path)
def word_to_html(self):
input_path = self.word_input_path_html.get()
output_path = self.html_output_path.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
soft = DispatchEx('Word.Application')
doc = soft.Documents.Open(input_path)
doc.SaveAs(output_path, FileFormat=10) # 10对应HTML格式
doc.Close()
soft.Quit()
messagebox.showinfo("成功", f"转换成功!文件已保存至:{output_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 3. Word转PDF
def init_word2pdf_tab(self):
frame = ttk.Frame(self.tab_word2pdf, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入Word文件:").grid(row=0, column=0, sticky="w", pady=5)
self.word_input_path_pdf = tk.StringVar()
ttk.Entry(frame, textvariable=self.word_input_path_pdf, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_word_input_pdf).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出PDF文件:").grid(row=1, column=0, sticky="w", pady=5)
self.pdf_output_path_word = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_output_path_word, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_output_word).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.word_to_pdf).grid(row=2, column=1, pady=20)
def browse_word_input_pdf(self):
path = filedialog.askopenfilename(filetypes=[("Word文件", "*.doc;*.docx")])
if path:
self.word_input_path_pdf.set(path)
def browse_pdf_output_word(self):
path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_output_path_word.set(path)
def word_to_pdf(self):
input_path = self.word_input_path_pdf.get()
output_path = self.pdf_output_path_word.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
# 验证输入文件是否存在
if not os.path.exists(input_path):
messagebox.showerror("错误", f"输入文件不存在 – {input_path}")
return
# 验证输入文件是否为Word文件
ext = os.path.splitext(input_path)[1].lower()
if ext not in ['.doc', '.docx']:
messagebox.showerror("错误", f"输入文件不是Word文件 – {input_path}")
return
# 处理输出路径
try:
output_dir = os.path.dirname(output_path)
if not output_dir:
output_dir = os.getcwd()
output_path = os.path.join(output_dir, output_path)
os.makedirs(output_dir, exist_ok=True)
except Exception as e:
messagebox.showerror("错误", f"处理输出路径时出错:{str(e)}")
return
# 确保输出文件扩展名为.pdf
if os.path.splitext(output_path)[1].lower() != '.pdf':
output_path += '.pdf'
word_app = None
try:
word_app = DispatchEx('Word.Application')
word_app.Visible = False
doc = word_app.Documents.Open(FileName=input_path, ReadOnly=True)
doc.SaveAs(FileName=output_path, FileFormat=17) # 17对应PDF格式
doc.Close(SaveChanges=False)
messagebox.showinfo("成功", f"转换成功!PDF文件已保存至:{output_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
finally:
if word_app:
word_app.Quit()
# 4. 图片转PDF
def init_img2pdf_tab(self):
frame = ttk.Frame(self.tab_img2pdf, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="图片文件夹:").grid(row=0, column=0, sticky="w", pady=5)
self.img_folder_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.img_folder_path, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_img_folder).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出PDF文件夹:").grid(row=1, column=0, sticky="w", pady=5)
self.pdf_folder_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_folder_path, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_folder).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.img_to_pdf).grid(row=2, column=1, pady=20)
def browse_img_folder(self):
path = filedialog.askdirectory()
if path:
self.img_folder_path.set(path)
def browse_pdf_folder(self):
path = filedialog.askdirectory()
if path:
self.pdf_folder_path.set(path)
def img_to_pdf(self):
img_path = self.img_folder_path.get()
pdf_path = self.pdf_folder_path.get()
if not img_path or not pdf_path:
messagebox.showerror("错误", "请选择图片文件夹和输出PDF文件夹")
return
# 确保输出目录存在
if not os.path.exists(pdf_path):
try:
os.makedirs(pdf_path)
except Exception as e:
messagebox.showerror("错误", f"创建输出目录失败:{str(e)}")
return
# 支持的图像文件扩展名
valid_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp'}
try:
file_list = os.listdir(img_path)
success_count = 0
error_count = 0
for filename in file_list:
ext = os.path.splitext(filename)[1].lower()
if ext in valid_extensions:
pdf_name = os.path.splitext(filename)[0]
img_file_path = os.path.join(img_path, filename)
try:
with Image.open(img_file_path) as im:
if not pdf_path.endswith(('\\\\', '/')):
pdf_path += os.sep
pdf_file_path = f"{pdf_path}{pdf_name}.pdf"
im.save(pdf_file_path, "PDF", resolution=100.0)
success_count += 1
except Exception as e:
error_count += 1
print(f"处理文件 {filename} 时出错: {str(e)}")
messagebox.showinfo("完成", f"转换完成!成功转换 {success_count} 个文件,失败 {error_count} 个文件")
except Exception as e:
messagebox.showerror("错误", f"转换过程出错:{str(e)}")
# 5. PDF转Word
def init_pdf2docx_tab(self):
frame = ttk.Frame(self.tab_pdf2docx, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入PDF文件:").grid(row=0, column=0, sticky="w", pady=5)
self.pdf_input_path_docx = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_input_path_docx, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_input_docx).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出Word文件:").grid(row=1, column=0, sticky="w", pady=5)
self.docx_output_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.docx_output_path, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_docx_output).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.pdf_to_docx).grid(row=2, column=1, pady=20)
def browse_pdf_input_docx(self):
path = filedialog.askopenfilename(filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_input_path_docx.set(path)
def browse_docx_output(self):
path = filedialog.asksaveasfilename(defaultextension=".docx", filetypes=[("Word文件", "*.docx")])
if path:
self.docx_output_path.set(path)
# 修改后的PDF转Word核心功能
def pdf_to_docx(self):
pdf_path = self.pdf_input_path_docx.get()
docx_path = self.docx_output_path.get()
if not pdf_path or not docx_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
# 创建PDF阅读器
reader = PdfReader(pdf_path)
# 创建Word文档
doc = Document()
# 提取每一页的文本并添加到Word中
total_pages = len(reader.pages)
for page_num, page in enumerate(reader.pages, 1):
try:
text = page.extract_text()
if text:
doc.add_paragraph(text)
# 每处理10页更新一次状态(大型PDF适用)
if page_num % 10 == 0:
print(f"已处理第 {page_num}/{total_pages} 页")
else:
print(f"第 {page_num} 页没有可提取的文本")
except Exception as e:
print(f"处理第 {page_num} 页时出错: {str(e)}")
continue
# 保存Word文档
doc.save(docx_path)
messagebox.showinfo("成功", f"转换完成,共处理 {total_pages} 页,文件保存至: {docx_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 6. PDF转图片
def init_pdf2img_tab(self):
frame = ttk.Frame(self.tab_pdf2img, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入PDF文件:").grid(row=0, column=0, sticky="w", pady=5)
self.pdf_input_path_img = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_input_path_img, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_input_img).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出图片文件夹:").grid(row=1, column=0, sticky="w", pady=5)
self.img_output_folder = tk.StringVar()
ttk.Entry(frame, textvariable=self.img_output_folder, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_img_output_folder).grid(row=1, column=2, padx=5)
ttk.Label(frame, text="Poppler路径:").grid(row=2, column=0, sticky="w", pady=5)
self.poppler_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.poppler_path, width=50).grid(row=2, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_poppler_path).grid(row=2, column=2, padx=5)
ttk.Label(frame, text="图片清晰度(DPI):").grid(row=3, column=0, sticky="w", pady=5)
self.img_dpi = tk.StringVar(value="500")
ttk.Entry(frame, textvariable=self.img_dpi, width=10).grid(row=3, column=1, sticky="w", pady=5)
ttk.Button(frame, text="转换", command=self.pdf_to_img).grid(row=4, column=1, pady=20)
def browse_pdf_input_img(self):
path = filedialog.askopenfilename(filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_input_path_img.set(path)
def browse_img_output_folder(self):
path = filedialog.askdirectory()
if path:
self.img_output_folder.set(path)
def browse_poppler_path(self):
path = filedialog.askdirectory()
if path:
self.poppler_path.set(path)
def pdf_to_img(self):
pdf_path = self.pdf_input_path_img.get()
output_folder = self.img_output_folder.get()
poppler_path = self.poppler_path.get()
try:
dpi = int(self.img_dpi.get())
except ValueError:
messagebox.showerror("错误", "请输入有效的清晰度数值")
return
if not pdf_path or not output_folder or not poppler_path:
messagebox.showerror("错误", "请填写所有必要路径")
return
# 确保输出目录存在
if not os.path.exists(output_folder):
try:
os.makedirs(output_folder)
except Exception as e:
messagebox.showerror("错误", f"创建输出目录失败:{str(e)}")
return
try:
# 获取PDF文件名(不含扩展名)
pdf_filename = os.path.splitext(os.path.basename(pdf_path))[0]
# 转换PDF为图片
pages = convert_from_path(
pdf_path,
dpi,
poppler_path=poppler_path
)
# 保存图片
for index, img in enumerate(pages):
img_path = os.path.join(output_folder, f'{pdf_filename}_{index + 1}.jpg')
img.save(img_path, 'JPEG')
messagebox.showinfo("成功", f"转换完成!共转换 {len(pages)} 页,保存至:{output_folder}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 7. PDF转文本
def init_pdf2txt_tab(self):
frame = ttk.Frame(self.tab_pdf2txt, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入PDF文件:").grid(row=0, column=0, sticky="w", pady=5)
self.pdf_input_path_txt = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_input_path_txt, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_input_txt).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出文本文件:").grid(row=1, column=0, sticky="w", pady=5)
self.txt_output_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.txt_output_path, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_txt_output).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.pdf_to_txt).grid(row=2, column=1, pady=20)
def browse_pdf_input_txt(self):
path = filedialog.askopenfilename(filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_input_path_txt.set(path)
def browse_txt_output(self):
path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("文本文件", "*.txt")])
if path:
self.txt_output_path.set(path)
def pdf_to_txt(self):
pdf_path = self.pdf_input_path_txt.get()
txt_path = self.txt_output_path.get()
if not pdf_path or not txt_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
# 打开pdf文件
with open(pdf_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
pages = len(pdf_reader.pages)
text = ""
# 循环遍历每一页,提取文本
for i in range(pages):
page = pdf_reader.pages[i]
text += page.extract_text()
# 保存为文本文件
with open(txt_path, 'w', encoding='utf-8') as file:
file.write(text)
messagebox.showinfo("成功", f"转换成功!文本文件已保存至:{txt_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 8. PDF转Excel
def init_pdf2excel_tab(self):
frame = ttk.Frame(self.tab_pdf2excel, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入PDF文件:").grid(row=0, column=0, sticky="w", pady=5)
self.pdf_input_path_excel = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_input_path_excel, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_input_excel).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出Excel文件:").grid(row=1, column=0, sticky="w", pady=5)
self.excel_output_path_pdf = tk.StringVar()
ttk.Entry(frame, textvariable=self.excel_output_path_pdf, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_excel_output_pdf).grid(row=1, column=2, padx=5)
ttk.Label(frame, text="解析模式:").grid(row=2, column=0, sticky="w", pady=5)
self.flavor = tk.StringVar(value="stream")
ttk.Combobox(frame, textvariable=self.flavor, values=["stream", "lattice"], width=10).grid(row=2, column=1,
sticky="w", pady=5)
ttk.Label(frame, text="处理页码(例如:1-5,all):").grid(row=3, column=0, sticky="w", pady=5)
self.pages = tk.StringVar(value="all")
ttk.Entry(frame, textvariable=self.pages, width=10).grid(row=3, column=1, sticky="w", pady=5)
ttk.Button(frame, text="转换", command=self.pdf_to_excel).grid(row=4, column=1, pady=20)
def browse_pdf_input_excel(self):
path = filedialog.askopenfilename(filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_input_path_excel.set(path)
def browse_excel_output_pdf(self):
path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel文件", "*.xlsx")])
if path:
self.excel_output_path_pdf.set(path)
def pdf_to_excel(self):
pdf_path = self.pdf_input_path_excel.get()
excel_path = self.excel_output_path_pdf.get()
flavor = self.flavor.get()
pages = self.pages.get()
if not pdf_path or not excel_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
# 读取PDF中的表格
data = camelot.read_pdf(pdf_path, pages=pages, flavor=flavor)
# 创建Excel工作簿
work = Workbook()
sheet = work.active
# 写入数据
for i in range(data.n):
for rows in data[i].data:
sheet.append(rows)
# 保存Excel文件
work.save(excel_path)
messagebox.showinfo("成功", f"转换成功!Excel文件已保存至:{excel_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 9. PPT转PDF
def init_ppt2pdf_tab(self):
frame = ttk.Frame(self.tab_ppt2pdf, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入PPT文件:").grid(row=0, column=0, sticky="w", pady=5)
self.ppt_input_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.ppt_input_path, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_ppt_input).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出PDF文件:").grid(row=1, column=0, sticky="w", pady=5)
self.pdf_output_path_ppt = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_output_path_ppt, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_output_ppt).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.ppt_to_pdf).grid(row=2, column=1, pady=20)
def browse_ppt_input(self):
path = filedialog.askopenfilename(filetypes=[("PPT文件", "*.ppt;*.pptx")])
if path:
self.ppt_input_path.set(path)
def browse_pdf_output_ppt(self):
path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_output_path_ppt.set(path)
def ppt_to_pdf(self):
input_path = self.ppt_input_path.get()
output_path = self.pdf_output_path_ppt.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
soft = DispatchEx('Powerpoint.Application')
doc = soft.Presentations.Open(input_path)
doc.SaveAs(output_path, FileFormat=32) # 32对应PDF格式
doc.Close()
soft.Quit()
messagebox.showinfo("成功", f"转换成功!文件已保存至:{output_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 10. Excel转CSV
def init_excel2csv_tab(self):
frame = ttk.Frame(self.tab_excel2csv, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入Excel文件:").grid(row=0, column=0, sticky="w", pady=5)
self.excel_input_path_csv = tk.StringVar()
ttk.Entry(frame, textvariable=self.excel_input_path_csv, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_excel_input_csv).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出CSV文件:").grid(row=1, column=0, sticky="w", pady=5)
self.csv_output_path = tk.StringVar()
ttk.Entry(frame, textvariable=self.csv_output_path, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_csv_output).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.excel_to_csv).grid(row=2, column=1, pady=20)
def browse_excel_input_csv(self):
path = filedialog.askopenfilename(filetypes=[("Excel文件", "*.xlsx;*.xls")])
if path:
self.excel_input_path_csv.set(path)
def browse_csv_output(self):
path = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV文件", "*.csv")])
if path:
self.csv_output_path.set(path)
def excel_to_csv(self):
input_path = self.excel_input_path_csv.get()
output_path = self.csv_output_path.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
data = pd.read_excel(input_path, index_col=0)
data.to_csv(output_path)
messagebox.showinfo("成功", f"转换成功!文件已保存至:{output_path}")
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
# 11. Excel转PDF
def init_excel2pdf_tab(self):
frame = ttk.Frame(self.tab_excel2pdf, padding=10)
frame.pack(fill="both", expand=True)
ttk.Label(frame, text="输入Excel文件:").grid(row=0, column=0, sticky="w", pady=5)
self.excel_input_path_pdf = tk.StringVar()
ttk.Entry(frame, textvariable=self.excel_input_path_pdf, width=50).grid(row=0, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_excel_input_pdf).grid(row=0, column=2, padx=5)
ttk.Label(frame, text="输出PDF文件:").grid(row=1, column=0, sticky="w", pady=5)
self.pdf_output_path_excel = tk.StringVar()
ttk.Entry(frame, textvariable=self.pdf_output_path_excel, width=50).grid(row=1, column=1, pady=5)
ttk.Button(frame, text="浏览…", command=self.browse_pdf_output_excel).grid(row=1, column=2, padx=5)
ttk.Button(frame, text="转换", command=self.excel_to_pdf).grid(row=2, column=1, pady=20)
def browse_excel_input_pdf(self):
path = filedialog.askopenfilename(filetypes=[("Excel文件", "*.xlsx;*.xls")])
if path:
self.excel_input_path_pdf.set(path)
def browse_pdf_output_excel(self):
path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF文件", "*.pdf")])
if path:
self.pdf_output_path_excel.set(path)
def excel_to_pdf(self):
input_path = self.excel_input_path_pdf.get()
output_path = self.pdf_output_path_excel.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
# 检查输入文件是否存在
if not os.path.exists(input_path):
messagebox.showerror("错误", f"Excel文件不存在 – {input_path}")
return
# 处理输出路径
try:
output_dir = os.path.dirname(output_path)
if not output_dir:
output_dir = os.getcwd()
output_path = os.path.join(output_dir, output_path)
os.makedirs(output_dir, exist_ok=True)
except Exception as e:
messagebox.showerror("错误", f"处理输出路径时出错:{str(e)}")
return
# 确保输出文件扩展名为.pdf
if os.path.splitext(output_path)[1].lower() != '.pdf':
output_path += '.pdf'
excel_app = None
try:
excel_app = DispatchEx('Excel.Application')
excel_app.Visible = False
excel_app.DisplayAlerts = False # 关闭所有弹窗
# 使用短路径
short_input = os.path.abspath(input_path)
short_output = os.path.abspath(output_path)
# 打开工作簿
workbook = excel_app.Workbooks.Open(
Filename=short_input,
ReadOnly=True,
UpdateLinks=0,
IgnoreReadOnlyRecommended=True
)
# 保存为PDF
workbook.ExportAsFixedFormat(
Type=0, # 0表示PDF格式
Filename=short_output,
Quality=0 # 0=标准质量
)
workbook.Close(SaveChanges=False)
messagebox.showinfo("成功", f"转换成功:{short_output}")
return True
except Exception as e:
messagebox.showerror("错误", f"转换失败:{str(e)}")
return False
finally:
if excel_app:
excel_app.Quit()
del excel_app # 释放资源
if __name__ == "__main__":
root = tk.Tk()
app = OfficeConverterApp(root)
root.mainloop()
源代码下载地址:https://download.csdn.net/download/qq_32257509/91673195
注意,使用本软件部分功能需要下载安装WPS2010软件。
评论前必须登录!
注册