2017-04-12 89 views
1

我创建了一个从csv文件读取信息并将其输出到docx文件的脚本。我的问题是,当docx文件保存时,它将其保存到我的python脚本所在的同一个文件夹中。我的桌面上有一个名为python的文件夹,在这个文件夹中我想保存docx文件。下面是我应该发生的一段脚本。谢谢你的帮助!Python 3:如何将docx文件保存到特定目录?

customer_list = r'C:\Users\path to csv file' 
    csv_file = read_emails(customer_list) #Function that turns csv into dictionary 

    for customer in csv_file: 
     word_template = r'C:\Users\path to word template' 
     document = Document(word_template) 

     customer_email = customer['email'] 
     customer_contact = customer['contact'] 

     document.add_paragraph(respond_by) 
     document.add_paragraph(date_sign) 

     terms = r'C:\Users\path to terms and conditions' 

     with open(terms,'r') as trms: 
      for line in trms: 
      document.add_paragraph(line) 

     filename = (customer_contact + '.docx') 

     document.save(filename) #Here I want to save to a different folder 

回答

1

而不是将只是一个filename,如果它不采取一个完整路径,修改Document.save方法采取完整文件路径和传递来代替。

filepath = r'C:\Users\desired path\' + filename 
document.save(filepath) 
+0

这工作,谢谢! – CrookTiny

相关问题