python批量读取Excel数据写入word

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}")

相关推荐

  1. python批量读取Excel数据写入word

    2024-07-11 15:02:05       22 阅读
  2. python读取excel数据写入mysql

    2024-07-11 15:02:05       24 阅读
  3. pythonexcel读取写入

    2024-07-11 15:02:05       16 阅读
  4. python&Pandas二:数据读取写入

    2024-07-11 15:02:05       55 阅读
  5. python按列写入数据excel

    2024-07-11 15:02:05       51 阅读
  6. Python】使用Python连接ClickHouse进行批量数据写入

    2024-07-11 15:02:05       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 15:02:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 15:02:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 15:02:05       45 阅读
  4. Python语言-面向对象

    2024-07-11 15:02:05       55 阅读

热门阅读

  1. 富格林:正确击破暗箱稳健出金

    2024-07-11 15:02:05       16 阅读
  2. 云原生监控-Kubernetes-Promethues-Grafana

    2024-07-11 15:02:05       12 阅读
  3. cmake

    2024-07-11 15:02:05       18 阅读
  4. leetcode300:最长递增子序列

    2024-07-11 15:02:05       19 阅读
  5. [Linux][Shell][Shell数学运算]详细讲解

    2024-07-11 15:02:05       19 阅读
  6. tessy 单元测试:小白入门指导手册

    2024-07-11 15:02:05       18 阅读
  7. C语言-概述,应用领域

    2024-07-11 15:02:05       16 阅读
  8. c++ 网络编程udp协议 poco模块

    2024-07-11 15:02:05       19 阅读