from docx import Document
from docx.shared import Pt
from docx.enum.table import WD_TABLE_ALIGNMENT, WD_ROW_HEIGHT_RULE
import os
import pandas as pd
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt
# ... 其他代码 ...
work_dir = 'path_to_your_excel_files'
excel_files = [f for f in os.listdir(
work_dir) if f.endswith(('.xlsx', '.xls'))]
# 创建一个新的Word文档
doc = Document()
# 遍历所有Excel文件
for excel_file in excel_files:
# ... 读取Excel文件并创建Word表格的代码 ...
excel_path = os.path.join(work_dir, excel_file)
# 读取Excel文件
df = pd.read_excel(excel_path)
# 将DataFrame转换为Word表格
for _, row in df.iterrows():
table = doc.add_table(rows=1, cols=len(row), style='Table Grid')
# 添加行并设置单元格数据
for i, value in enumerate(row):
cell = table.cell(0, i)
cell.text = str(value)
# cell.vertical_alignment = 'center' # 垂直居中对齐
# 设置表格样式
table.alignment = WD_TABLE_ALIGNMENT.CENTER
# 设置行高
for row in table.rows:
row.height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
row.height = Pt(20)
# 保存Word文档
# ... 保存文档的代码 ...
output_path = os.path.join(work_dir, 'CombinedTables.docx')
doc.save(output_path)
print(f"Word文档已保存至:{output_path}")
python批量读取Excel数据写入word
2024/10/24 4:48:28
来源:https://blog.csdn.net/cc605523/article/details/140229644
浏览:
次
关键词:python批量读取Excel数据写入word
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com
热文排行
- npm install puppeteer 报错 npm ERR! PUPPETEER_DOWNLOAD_HOST is deprecated解决办法
- 《缺失MRI模态下的脑肿瘤分割的潜在相关表示学习》| 文献速递-深度学习肿瘤自动分割
- 基于重要抽样的主动学习不平衡分类方法ALIS
- 【微信小程序】自定义组件 - 组件的生命周期
- Android Studio Build窗口出现中文乱码问题
- ray框架Tune和Train区别
- Datawhale AI 夏令营-CV竞赛-Task2
- 概率图模型在自然语言处理中的应用
- 黑神话:悟空游戏用的什么服务器?
- CCF GESP Python编程 一级认证真题 2024年6月