本文目录导读:

- Python + python-docx(处理Word文档)
- Python + pandas(处理Excel/CSV)
- Python + pdfplumber(处理PDF)
- 批量转换脚本(多种格式)
- 命令行工具(使用LibreOffice)
- 使用pandoc(通用文档转换)
- 简单Shell脚本
- 使用建议
我来介绍几种常用的文档格式转换脚本方法,根据你的具体需求选择:
Python + python-docx(处理Word文档)
# 安装:pip install python-docx
from docx import Document
# Word转文本
def docx_to_txt(input_file, output_file):
doc = Document(input_file)
text = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
with open(output_file, 'w', encoding='utf-8') as f:
f.write(text)
print(f"转换完成: {output_file}")
# 创建Word文档
def txt_to_docx(input_file, output_file):
doc = Document()
with open(input_file, 'r', encoding='utf-8') as f:
for line in f:
doc.add_paragraph(line.strip())
doc.save(output_file)
print(f"转换完成: {output_file}")
Python + pandas(处理Excel/CSV)
# 安装:pip install pandas
import pandas as pd
# Excel转CSV
def excel_to_csv(input_file, output_file, sheet_name=0):
df = pd.read_excel(input_file, sheet_name=sheet_name)
df.to_csv(output_file, index=False, encoding='utf-8-sig')
print(f"转换完成: {output_file}")
# CSV转Excel
def csv_to_excel(input_file, output_file):
df = pd.read_csv(input_file, encoding='utf-8')
df.to_excel(output_file, index=False)
print(f"转换完成: {output_file}")
# 多Sheet合并
def merge_excel_sheets(input_file, output_file):
all_data = pd.read_excel(input_file, sheet_name=None)
merged = pd.concat(all_data.values(), ignore_index=True)
merged.to_excel(output_file, index=False)
Python + pdfplumber(处理PDF)
# 安装:pip install pdfplumber
import pdfplumber
# PDF转文本
def pdf_to_txt(input_file, output_file):
with pdfplumber.open(input_file) as pdf:
text = ''
for page in pdf.pages:
text += page.extract_text() + '\n'
with open(output_file, 'w', encoding='utf-8') as f:
f.write(text)
print(f"转换完成: {output_file}")
批量转换脚本(多种格式)
import os
import shutil
from pathlib import Path
class DocumentConverter:
def __init__(self):
self.conversion_map = {
'.docx': self.docx_to_txt,
'.txt': self.txt_to_docx,
'.xlsx': self.excel_to_csv,
'.csv': self.csv_to_excel,
}
def convert_file(self, input_path, target_format):
# 检查文件是否存在
if not Path(input_path).exists():
raise FileNotFoundError(f"文件不存在: {input_path}")
# 获取文件信息
input_path = Path(input_path)
output_path = input_path.with_suffix(target_format)
# 根据扩展名选择转换方法
if input_path.suffix.lower() in ['.docx', '.doc']:
self.docx_to_txt(input_path, output_path)
elif input_path.suffix.lower() in ['.xlsx', '.xls']:
self.excel_to_csv(input_path, output_path)
elif input_path.suffix.lower() in ['.pdf']:
self.pdf_to_txt(input_path, output_path)
return output_path
def batch_convert(self, directory, target_format, recursive=False):
# 批量转换目录下的所有文件
results = []
pattern = '*.*' if recursive else '*'
for file_path in Path(directory).glob(pattern):
if file_path.is_file():
try:
output = self.convert_file(file_path, target_format)
results.append(output)
except Exception as e:
print(f"转换失败 {file_path}: {e}")
return results
# 使用示例
converter = DocumentConverter()
# 转换单个文件
converter.convert_file('example.docx', '.txt')
# 批量转换目录
converter.batch_convert('./documents', '.txt', recursive=True)
命令行工具(使用LibreOffice)
# 批量转换Word/Excel/PPT为PDF soffice --headless --convert-to pdf *.docx *.xlsx *.pptx --outdir output_directory/ # 转换HTML为PDF soffice --headless --convert-to pdf file.html --outdir output_directory/
使用pandoc(通用文档转换)
# 安装:pip install pypandoc
import pypandoc
# 各种格式转换
def convert_with_pandoc(input_file, output_file, format_type=None):
# 支持格式: markdown, html, docx, pdf, rst, latex等
output = pypandoc.convert_file(input_file, 'docx', outputfile=output_file)
print(f"转换完成: {output_file}")
# 批量转换
def batch_convert_markdown(directory):
for md_file in Path(directory).glob('*.md'):
convert_with_pandoc(str(md_file), str(md_file.with_suffix('.docx')))
简单Shell脚本
#!/bin/bash
# convert_formats.sh
# 将文件名编码转换
iconv -f GBK -t UTF-8 input.txt > output.txt
# 批量重命名文件扩展名
for file in *.txt; do
mv "$file" "${file%.txt}.md"
done
# 批量转换编码
for file in *.txt; do
iconv -f GBK -t UTF-8 "$file" > "${file%.txt}_utf8.txt"
done
使用建议
-
根据文件类型选择:
- Word文档 → python-docx
- Excel表格 → pandas
- PDF → pdfplumber/PyPDF2
- 通用格式 → pandoc
-
批量处理建议:
- 使用路径通配符进行批量处理
- 添加错误处理和日志记录
- 合理使用并行处理提高效率
-
保持原格式:
- 简单文本转换用txt格式
- 复杂样式用pdf或docx格式
- 数据表格用csv/excel格式
需要我针对特定格式转换写更详细的脚本吗?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播