├── LICENSE ├── README.md ├── auto-excel-to-word.py ├── data.xlsx ├── requirements.txt └── temp.docx /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Zhang Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 安装使用 2 | ``` 3 | git clone https://github.com/rmboot/auto-excel-to-word.git 4 | cd auto-excel-to-word 5 | pip install -r requirements.txt 6 | python auto-excel-to-word.py 7 | ``` 8 | 9 | # 运行过程 10 | ###### 1.pandas读取存储数据的excel表格(data.xlsx),并将其转换为存储字典(dict)的列表(list)类型。 11 | ###### 2.循环读取列表(list)的每项数据(即data.xlsx的每一行),此时为字典(dict)。 12 | ###### 3.docxtpl加载模板文件(temp.docx)并按字典(dict)填充模板的内容。 13 | ###### 4.auto-excel-to-word目录内生成相应的word文件。 14 | -------------------------------------------------------------------------------- /auto-excel-to-word.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from docxtpl import DocxTemplate 3 | 4 | data = pd.read_excel('data.xlsx') 5 | dict_list = data.to_dict(orient="records") 6 | """ 7 | dict_list = [{'姓名': '张三', '性别': '男', '专业': '网络工程'}, 8 | {'姓名': '李四', '性别': '女', '专业': '法学'}, 9 | {'姓名': '王五', '性别': '男', '专业': '生物工程'}] 10 | """ 11 | for i in dict_list: 12 | tpl = DocxTemplate('temp.docx') 13 | tpl.render(i) 14 | tpl.save(i['姓名']+'.docx') 15 | -------------------------------------------------------------------------------- /data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmboot/auto-excel-to-word/d90b16c5ccb35c86424a233ca61863f0671e5682/data.xlsx -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pandas==1.0.0 2 | docxtpl==0.6.3 3 | -------------------------------------------------------------------------------- /temp.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmboot/auto-excel-to-word/d90b16c5ccb35c86424a233ca61863f0671e5682/temp.docx --------------------------------------------------------------------------------