Wie erstelle ich einen Text in dieser PDF -Datei und speichere ihn mit Python auf dem Desktop? [geschlossen]

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie erstelle ich einen Text in dieser PDF -Datei und speichere ihn mit Python auf dem Desktop? [geschlossen]

by Anonymous » 05 Apr 2025, 23:07

Ich erstelle einen MCQ -Generator mit AI (für Fragen basierend auf dem angegebenen Absatz) und möchte diesen AI -zurückgegebenen Text in eine PDF -Datei speichern und im Desktop des Benutzers speichern, aber ich erhalte kein Verzeichnis /kein Berechtigungsfehler.

Code: Select all

from reportlab.genpdf import canvas

def pdf_file_question_write(self, widget):
try:
os_name = platform.system()
if os_name == "Windows":
desktop_path = os.path.join(os.environ['USERPROFILE'], 'Desktop')
elif os_name in ["Linux", "Darwin"]:  # macOS is "Darwin"
desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
elif "ANDROID_BOOTLOGO" in os.environ:
# If running on Android, use the Downloads folder
desktop_path = "/storage/emulated/0/Download"
else:
raise Exception("Unsupported operating system")

output_path = os.path.join(desktop_path, "MCQ Generator.pdf")

# Create a PDF and add text
pdf = canvas.Canvas(output_path)
pdf.setFont("Helvetica", 12)
pdf.drawString(100, 800, "Generated Questions:")  # Title
text_lines = str(self.Generated_Question).split("\n")  # Split text into lines
y = 780  # Start position for text
for line in text_lines:
pdf.drawString(100, y, line)
y -= 15  # Move down for the next line
pdf.save()  # Save the PDF
print(f"PDF saved successfully at: {output_path}")

except Exception as e:
print(f"An error occurred: {str(e)}")

Top