Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
tại sao khi mình chuyển đổi thì lại ra một file trắng không có chữ ạ
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()
12345678910111213141516171819202122232425262728293031323334353637383940
tại sao khi mình chuyển đổi thì lại ra một file trắng không có chữ ạ
Để sử dụng tính năng này, Bạn cần đăng nhập bằng tài khoản Howkteam.
Đăng nhập
Để xem được nội dung và giúp Kteam duy trì hoạt động.
Bạn vui lòng tắt Adblock trên website howkteam.vn nhé!
Mình nghĩ là do tham số không được chỉ định đúng trong hàm
c.drawString,
bạn thử xem lại toạ độ x, y đi