Мне нужно спросить пользователя, где сохранить файл PDF и как назвать его с помощью Python - PullRequest
0 голосов
/ 04 мая 2019

Я новичок в Python, очень начальный уровень.Я использовал существующее руководство, чтобы создать программу, которая запрашивала у конечного пользователя какие-либо данные, а затем сохраняла их в формате PDF.

имя файла, в котором оно сохранено, и местоположение предварительно определено.Мне нужно, чтобы она спросила пользователя об этой информации, но я не знаю, как ее кодировать.

Я видел это сообщение здесь, но я не знал, как его использовать.

  60   def asksaveasfile(self):
  61 
  62     """Returns an opened file in write mode."""
  63 
  64     return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)
  65 
  66   def asksaveasfilename(self):
  67 
  68     """Returns an opened file in write mode.
  69     This time the dialog just returns a filename and the file is opened by your own code.
  70     """

import os
import pdfrw

# This is what I think would need to change but if this changes here 
# I would imagin the If statment at the bottom would have to change as well.
INVOICE_TEMPLATE_PATH = 'C:\Python\invoice_template.pdf'
INVOICE_OUTPUT_PATH = 'C:\Python\invoice.pdf'


ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'


def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    annotations = template_pdf.pages[0][ANNOT_KEY]
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation[ANNOT_FIELD_KEY][1:-1]
                if key in data_dict.keys():
                    annotation.update(
                        pdfrw.PdfDict(V='{}'.format(data_dict[key]))
                    )
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)



data_dict = {
   'tech_name': input("Enter your name: \n\t"),
   'date': input("Enter todays date: \n\t"),
   'user_name': input("Enter the end users name: \n\t"),
   'phone_number': input("Enter their phone number name: \n\t"),
   'old_tag': input("Enter the old computers asset tag: \n\t"),
   'old_serial': input("Enter the old computers serial number: \n\t"),
   'new_tag': input("Enter the new computers asset tag: \n\t"),
   'new_serial': input("Enter the new serial number: \n\t")
}

print("\n\nDone!\n\n")

if __name__ == '__main__':
    write_fillable_pdf(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH, data_dict)

На данный момент этот код работает.Только не так, как мне нужно, чтобы это работало.

...