Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 1.pdf
Binary file not shown.
194 changes: 193 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,193 @@
# csv-word
# 文档自动化工具

基于 Python + python-docx + PyPDF2 开发的文档自动化工具,支持从 CSV 数据生成 Word 报告、Word 转 PDF、PDF 合并等功能。

## 功能特性

- 从 CSV 读取数据,自动生成 Word 报告(包含标题、段落、表格)
- 将 Word 文档转换为 PDF
- 支持合并多个 PDF 文件
- 命令行参数控制:报告模板、输入数据、输出路径
- 完善的异常处理:文件不存在、格式不兼容时友好提示

## 项目结构

```
csv-word/
├── main.py # 主程序入口
├── word_generator.py # Word 文档生成模块
├── pdf_processor.py # PDF 处理模块
├── sample_data.csv # 示例 CSV 数据
├── requirements.txt # 依赖列表
└── README.md # 使用说明
```

## 安装

### 1. 安装 Python 依赖

```bash
pip install -r requirements.txt
```

### 2. 安装 Word 转 PDF 所需软件

**Windows 用户:**
- 需要安装 Microsoft Word(程序会通过 COM 接口调用)
- 或安装 LibreOffice 并添加到系统 PATH

**Linux/macOS 用户:**
- 安装 LibreOffice:
- Ubuntu/Debian: `sudo apt install libreoffice`
- macOS: `brew install --cask libreoffice`

## 使用方法

### 查看帮助

```bash
python main.py --help
python main.py generate --help
python main.py convert --help
python main.py merge --help
```

### 1. 从 CSV 生成 Word 报告

```bash
# 基本用法
python main.py generate -i sample_data.csv -o report.docx

# 指定报告标题
python main.py generate -i sample_data.csv -o report.docx --title "员工信息报告"

# 使用模板文件
python main.py generate -i sample_data.csv -o report.docx -t template.docx

# 指定 CSV 编码(解决中文乱码问题)
python main.py generate -i sample_data.csv -o report.docx --encoding gbk

# 生成后自动转换为 PDF
python main.py generate -i sample_data.csv -o report.docx --to-pdf
```

### 2. Word 文档转 PDF

```bash
# 基本用法(输出到同目录)
python main.py convert -i report.docx

# 指定输出路径
python main.py convert -i report.docx -o output/report.pdf
```

### 3. 合并多个 PDF 文件

```bash
# 合并多个 PDF
python main.py merge -i file1.pdf file2.pdf file3.pdf -o merged.pdf

# 使用通配符(PowerShell)
python main.py merge -i (Get-ChildItem *.pdf).FullName -o merged.pdf
```

## 运行示例

### 完整流程示例

```bash
# 1. 安装依赖
pip install -r requirements.txt

# 2. 使用示例数据生成报告
python main.py generate -i sample_data.csv -o employee_report.docx --title "员工信息报告"

# 3. 转换为 PDF
python main.py convert -i employee_report.docx -o employee_report.pdf

# 4. 或者一步完成(生成并转 PDF)
python main.py generate -i sample_data.csv -o employee_report.docx --title "员工信息报告" --to-pdf
```

## CSV 文件格式要求

CSV 文件第一行作为表头,后续每行作为数据记录:

```csv
姓名,部门,职位,入职日期,月薪
张三,技术部,高级工程师,2020-03-15,25000
李四,市场部,市场经理,2019-07-20,22000
```

## 常见问题

### 1. 中文乱码问题

如果 CSV 文件包含中文且出现乱码,尝试指定编码:

```bash
python main.py generate -i data.csv -o report.docx --encoding gbk
# 或
python main.py generate -i data.csv -o report.docx --encoding utf-8-sig
```

### 2. Word 转 PDF 失败

**Windows:**
- 确保已安装 Microsoft Word
- 如果使用 LibreOffice,确保已添加到系统 PATH

**Linux/macOS:**
- 确保已安装 LibreOffice
- 验证安装:`libreoffice --version` 或 `soffice --version`

### 3. 文件不存在错误

检查文件路径是否正确,支持相对路径和绝对路径:

```bash
# 相对路径
python main.py generate -i ./data/sample.csv -o ./output/report.docx

# 绝对路径
python main.py generate -i C:\Users\YourName\data\sample.csv -o C:\Users\YourName\output\report.docx
```

## API 使用

除了命令行,也可以在 Python 代码中直接调用模块:

```python
from word_generator import WordGenerator, generate_report_from_csv
from pdf_processor import PDFProcessor, convert_word_to_pdf, merge_pdf_files

# 生成 Word 报告
word_path = generate_report_from_csv(
csv_path='data.csv',
output_path='report.docx',
title='数据报告'
)

# Word 转 PDF
pdf_path = convert_word_to_pdf('report.docx', 'report.pdf')

# 合并 PDF
merged_path = merge_pdf_files(['file1.pdf', 'file2.pdf'], 'merged.pdf')

# 使用 WordGenerator 类进行更灵活的操作
generator = WordGenerator() # 或 WordGenerator('template.docx')
generator.add_title('自定义报告', level=0)
generator.add_paragraph('这是一段描述文字。')
generator.add_table_from_csv('data.csv', title='数据表格')
generator.save('custom_report.docx')
```

## 依赖说明

- **python-docx**: Word 文档创建和编辑
- **PyPDF2**: PDF 文件读取和合并
- **pywin32** (Windows): 通过 COM 接口调用 Microsoft Word

## 许可证

MIT License
Binary file added __pycache__/pdf_processor.cpython-314.pyc
Binary file not shown.
Binary file added __pycache__/word_generator.cpython-314.pyc
Binary file not shown.
Binary file added employee_report.docx
Binary file not shown.
Binary file added employee_report.pdf
Binary file not shown.
Binary file added employee_report1.pdf
Binary file not shown.
Loading