1、批量转换.doc文件为.docx文件
import os
from win32com import client as wc
def findext(dir, ext):
allfile = os.listdir(dir)
files_filter = filter(lambda x: x.endswith(ext), allfile)
list2 = list(files_filter)
return list2
if __name__ == '__main__':
indir = r'C:\Users\tony\OneDrive\书\电子\Python自动化办公\code\chapter7\data\test\in'
outdir = r'C:\Users\tony\OneDrive\书\电子\Python自动化办公\code\chapter7\data\test\out'
wordapp = wc.Dispatch("Word.Application")
list2 = findext(indir, '.doc')
for name in list2:
infile = os.path.join(indir, name)
name = name.replace('.doc', '.docx')
outfile = os.path.join(outdir, name)
document = wordapp.Documents.Open(infile)
document.SaveAs(outfile, FileFormat=12)
print(outfile, "转换OK。")
document.Close(0)
print("Game Over!")
2、采用模板批量生成证书文件
from datetime import datetime
import xlwings as xw
from docxtpl import DocxTemplate
''' # 读取Excel文件获得员工数据 '''
def readdata():
app = xw.App(visible=False, add_book=False)
f = r'data/员工信息.xlsx'
print('打开文件:', f)
wb = app.books.open(f)
sheet1 = wb.sheets[0]
rng = sheet1.range('B2').expand('down')
names = rng.value
wb.close()
app.quit()
print('关闭文件:', f)
return names
if __name__ == '__main__':
'''获得员工姓名列表 '''
emps = readdata()
date = datetime.now().strftime('%Y-%m-%d')
outdir = r'C:\Users\tony\OneDrive\书\电子\Python自动化办公\code\chapter7\data\test\out'
for emp in emps:
tpl = DocxTemplate('data/证书模板.docx')
context = {}
context['name'] = emp
context['title'] = '关总经理'
context['date'] = date
tpl.render(context)
file = ('{0}/证书-{1}.docx'.format(outdir, emp))
tpl.save(file)
print('{0}证书生成完成。'.format(emp))
print('Game Over!')
3、批量统计文件页数和字体数
import os
from win32com.client import Dispatch
def findext(dir, exts):
allfile = os.listdir(dir)
files_filter = filter(lambda x: os.path.splitext(x)[1] in exts, allfile)
list2 = list(files_filter)
return list2
if __name__ == '__main__':
indir = r'C:\Users\tony\OneDrive\书\电子\Python自动化办公\code\chapter7\data\test\in'
list2 = findext(indir, ['.doc', '.docx'])
wordapp = Dispatch('Word.Application')
wordapp.Visible = False
for name in list2:
infile = os.path.join(indir, name)
document = wordapp.Documents.Open(infile)
document.Repaginate()
pagenum = document.ComputeStatistics(2)
wordnum = document.ComputeStatistics(6)
print('文{0}文件:页数是:{1},字符数:{2}'.format(name, pagenum, wordnum))
document.Close(0)
wordapp.Quit()
print("Game Over!")
4、批量转换Word文件为PDF文件
import os
from win32com import client as wc
def findext(dir, exts):
allfile = os.listdir(dir)
files_filter = filter(lambda x: os.path.splitext(x)[1] in exts, allfile)
list2 = list(files_filter)
return list2
if __name__ == '__main__':
indir = r'C:\Users\tony\OneDrive\书\电子\Python自动化办公\code\chapter7\data\test\in'
outdir = r'C:\Users\tony\OneDrive\书\电子\Python自动化办公\code\chapter7\data\test\out'
wordapp = wc.Dispatch("Word.Application")
list2 = findext(indir, ['.doc', '.docx'])
for name in list2:
infile = os.path.join(indir, name)
name = name.replace('.docx', '.pdf')
name = name.replace('.doc', '.pdf')
outfile = os.path.join(outdir, name)
document = wordapp.Documents.Open(infile)
document.SaveAs(outfile, FileFormat=17)
print(outfile, "转换OK。")
document.Close(0)
wordapp.Quit()
print("Game Over!")
参考:《趣玩python:自动化办公真简单》p176
|