gpt4 book ai didi

python 3 : How do I save a docx file to a specific directory?

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:38 24 4
gpt4 key购买 nike

我创建了一个脚本,用于从 csv 文件读取信息并将其输出到 docx 文件。我的问题是,当 docx 文件保存时,它会将其保存到我的 python 脚本所在的同一文件夹中。我的桌面上有一个名为 python 的文件夹,我想在这个文件夹中保存 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

最佳答案

与其只传递一个文件名,如果它还没有完整路径,修改Document.save 方法以获取完整文件路径并传递它.

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

关于 python 3 : How do I save a docx file to a specific directory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43373648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com