简体   繁体   English

使用 pandas 列表和 python docx 命名和保存 docx 文件

[英]Naming and Saving docx file using pandas list and python docx

I use pandas to get list of employee names from excel sheet and want to create docx file for each name in cell as docx filename.我使用 pandas 从 excel 表中获取员工姓名列表,并希望为单元格中的每个名称创建 docx 文件作为 docx 文件名。 I got 10 names and i need to create 10 docx file automatically.我有 10 个名字,我需要自动创建 10 个 docx 文件。

Current effort: Learnt to read the names and create a docx file.目前的努力:学会阅读名称并创建一个 docx 文件。

from docx import Document
import pandas
df = pandas.read_excel(open('test.xlsx','rb'))
print(df)
document = Document()
document.save('Name of the relevant cells.docx')

How to read through each cell value and pass it on to the file so as to create 10 docx file wit h relavant name.如何读取每个单元格值并将其传递给文件,以创建 10 个具有相关名称的 docx 文件。

Contents of test.xlsx test.xlsx 的内容

Names名称

Adam亚当

Smith史密斯

John约翰

Mark标记

Please help with alphanumeric also like Place_postalcode in case some future necessity occurs.请帮助使用字母数字,例如 Place_postalcode,以防将来发生某些必要性。

You should be able to loop through the names in the df['Names'] and create a Document with the name您应该能够遍历 df['Names'] 中的名称并创建一个名称为Document

Test data:测试数据:

在此处输入图像描述

Code:代码:

df = pd.read_excel(open('test.xlsx','rb'))
for name in df.Names:
    document = Document()
    document.save(str(name)+'.docx')

Result:结果:

在此处输入图像描述

Converting the name to string will handle both numeric and alpha-numeric file names.将名称转换为字符串将同时处理数字和字母数字文件名。 You can also have the file names with space.您还可以将文件名带有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM