Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
from pptx import Presentation
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from io import BytesIO
Load the PowerPoint presentation
pptx_file = "Hoc_ngu_phap_tieng_Nhat_Minna_no_Nihongo.pptx"
prs = Presentation(pptx_file)
Create PDF file
pdf_file = "Hoc_ngu_phap_tieng_Nhat_Minna_no_Nihongo.pdf"
pdf_buffer = BytesIO()
c = canvas.Canvas(pdf_buffer, pagesize=letter)
Function to draw text with proper formatting
def draw_text(text, x, y, font_size=12, max_width=500):
c.setFont("Helvetica", font_size)
lines = text.split("\n")
y_position = y
for line in lines:
if c.stringWidth(line, "Helvetica", font_size) > max_width:
parts = []
part = ''
words = line.split()
for word in words:
if c.stringWidth(part + ' ' + word, "Helvetica", font_size) <= max_width:
part += ' ' + word
else:
parts.append(part)
part = word
if part:
parts.append(part)
for part in parts:
c.drawString(x, y_position, part.strip())
y_position -= 1.2 * font_size
else:
c.drawString(x, y_position, line.strip())
y_position -= 1.2 * font_size
Iterate through slides and draw content to PDF
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
if shape.has_text_frame:
text_frame = shape.text_frame
paragraphs = text_frame.paragraphs
for paragraph in paragraphs:
text = paragraph.text
draw_text(text, 50, 750) # Adjust position as needed
c.save()
Write PDF buffer to file
with open(pdf_file, "wb") as f:
f.write(pdf_buffer.getvalue())
print(f"File PDF đã được tạo thành công: {pdf_file}")