Change the save location of a Python PDF (pdf.save())

Asked

Viewed 128 times

-1

I made a program in Python that at the end generates a PDF with the following data:

from reportlab.pdfgen import canvas

    pdf = canvas.Canvas("vendas_produtos.pdf")
    pdf.setFont("Times-Bold", 14)
    pdf.drawString(200, 800, "Produtos vendidos:")
    pdf.setFont("Times-Bold", 10)

    pdf.drawString(10, 750, "ID")
    pdf.drawString(60, 750, "Descrição")
    pdf.drawString(230, 750, "Preço")
    pdf.drawString(310, 750, "Dia")
    pdf.drawString(340, 750, "Mês")
    pdf.drawString(370, 750, "Quant.")
    pdf.drawString(420, 750, "L. u.(%)")
    pdf.drawString(470, 750, "L. u.(R$)")
    pdf.drawString(520, 750, "L. Tot.(R$)")
    pdf.drawString(400, 800, "Lucro Total:")
    pdf.drawString(470, 800, f"{moeda(lucro_total)}")

    for i in range(0, len(gerar_pdf_dados)):
        y += 50
        pdf.drawString(10, 750 - y, str(gerar_pdf_dados[i][0]))
        pdf.drawString(60, 750 - y, str(gerar_pdf_dados[i][1]))
        pdf.drawString(230, 750 - y, str(gerar_pdf_dados[i][2]))
        pdf.drawString(310, 750 - y, str(gerar_pdf_dados[i][3]))
        pdf.drawString(340, 750 - y, str(gerar_pdf_dados[i][4]))
        pdf.drawString(370, 750 - y, str(gerar_pdf_dados[i][5]))
        pdf.drawString(420, 750 - y, str(gerar_pdf_dados[i][6]))
        pdf.drawString(470, 750 - y, str(gerar_pdf_dados[i][7]))
        pdf.drawString(520, 750 - y, str(gerar_pdf_dados[i][8]))
    pdf.save()

I wanted to save this file on the desktop and not in the program folder, how can I do this?

  • just save to that folder and use copy and send to any other folder, already tried it, by the way this method by what I saw on the Internet only does it

  • It is because when I turn the program into . exe error when generating the pdf, so I wanted to put a command to save on desktop, understood?

  • But there is another problem, what is the error:? that you did not inform in the question and then when it closes you complain

  • When this happens not from the error, just does not generate the pdf, what I asked in the question is to change the save location.

  • As I said Dirso this method does not have that, if you have to do for the IO class responsible (I do not know if that’s the name) of python

  • All right. I’ll try :/

  • Example: https://stackoverflow.com/a/123212/6797930

Show 2 more comments

2 answers

1


just include the path in the file name setting:

pdf = canvas.Canvas("C:/Users/seu_usuario/Área de Trabalho/vendas_produtos.pdf")
  • Thank you! It has how I put the location to be on the desktop regardless of which user?

  • 1

    Usually the desktop is linked to a user, maybe if you create a shared folder it might solve, so all users would have access to the file.

  • Just complementing the answer: you can grab the current user folder with the method Path.home() module pathlib. Ex.: from pathlib import Path; desktop = Path.home() / "Área de Trabalho" / "vendas_produtos.pdf".

-2

import getpass
from reportlab.pdfgen import canvas 
from reportlab.lib.pagesizes import A4

user_windows = getpass.getuser()
cnv = canvas.Canvas(f'C:\\Users\\{user_windows}\\Desktop\\report.pdf', pagesize=A4)

see if it helps you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.