from tkinter import *
from tkinter import filedialog
import PyPDF2
from reportlab.pdfgen import canvas
import codecs
def pdf_to_word():
pdf_file = filedialog.askopenfilename(title = "Chọn tệp PDF", filetypes = (("PDF files", "*.pdf"), ("all files", "*.*")))
try:
pdf_reader = PyPDF2.PdfFileReader(open(pdf_file, 'rb'))
except Exception as e:
print("Lỗi khi mở tệp PDF: ", e)
return
word_file = filedialog.asksaveasfilename(title = "Lưu tệp Word", defaultextension=".txt", filetypes = (("Text files", "*.txt"), ("all files", "*.*")))
with open(word_file, 'w', encoding='utf-8') as text_file:
for page in range(pdf_reader.numPages):
text = pdf_reader.getPage(page).extractText()
text_file.write(text)
def word_to_pdf():
word_file = filedialog.askopenfilename(title = "Chọn tệp Word", filetypes = (("Text files", "*.txt"), ("all files", "*.*")))
pdf_file = filedialog.asksaveasfilename(title = "Lưu tệp PDF", defaultextension=".pdf", filetypes = (("PDF files", "*.pdf"), ("all files", "*.*")))
c = canvas.Canvas(pdf_file)
with open(word_file, 'r', encoding='utf-8') as text_file:
text = text_file.read()
c.drawString(72, 720, text)
c.save()
root = Tk()
root.title("Chuyển đổi PDF - Word")
pdf_to_word_button = Button(root, text = "PDF to Word", command = pdf_to_word)
pdf_to_word_button.pack()
word_to_pdf_button = Button(root, text = "Word to PDF", command = word_to_pdf)
word_to_pdf_button.pack()
root.mainloop()
tại sao khi mình chuyển đổi file word ra pdf và ngược lại thì lại ra một file trắng ạ