我需要问用户将pdf文件保存在哪里以及如何使用python命名它

时间:2019-05-04 19:37:53

标签: python saving-data

我是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)

到目前为止,此代码有效。只是不是我需要它如何工作。

0 个答案:

没有答案