Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evgo2017 committed May 13, 2019
0 parents commit c5ead2c
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 evgo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
166 changes: 166 additions & 0 deletions Office2PDF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package test;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.*;

public class test {
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32;

public static void main(String[] args) {
//放置需转换的文件的 file 文件夹目录
String filepath = "C://test";
// 获得指定文件对象
File file = new File(filepath);
// 获得该文件夹内的所有文件
File[] fileList = file.listFiles();
System.out.println("开始\n====================");
long start = System.currentTimeMillis();
for(int i = 0; i<fileList.length; i++) {
test.convert2PDF(fileList[i].toString(), changeFileSufix2PDF(fileList[i].toString()));
}
long end = System.currentTimeMillis();
System.out.println("\n====================\n全部转换完成..用时:" + (end - start) + "ms.");
System.out.println("====================\n结束");
}
public static boolean convert2PDF(String inputFile, String pdfFile) {
// 获得文件
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists()) {
return false;
}
if (suffix.equals("pdf")) {
return false;
}
if (suffix.equals("doc") || suffix.equals("docx")
|| suffix.equals("txt")) {
return word2PDF(inputFile, pdfFile);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return ppt2PDF(inputFile, pdfFile);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excel2PDF(inputFile, pdfFile);
} else {
return false;
}
}
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
public static String changeFileSufix2PDF(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(0, splitIndex) + ".pdf";
}
// word转换为pdf
public static boolean word2PDF(String inputFile, String pdfFile) {
System.out.println("\n====================\n启动Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch docs = null;
Dispatch doc = null;
try {
// 打开word应用程序
app = new ActiveXComponent("Word.Application");
// 设置word不可见
app.setProperty("Visible", false);
// 获得word中所有打开的文档,返回Documents对象
System.out.println("打开文档..." + inputFile);
docs = app.getProperty("Documents").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
doc = Dispatch.call(docs, "Open", inputFile, false, true)
.toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
/*
* Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
* //word保存为pdf格式宏,值为17 );
*/
// word保存为pdf格式宏,值为17
System.out.println("转换文档到PDF..." + pdfFile);
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);
// 关闭文档
Dispatch.call(doc, "Close", false);
// 关闭word应用程序
app.invoke("Quit", 0);

long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.\n====================");

return true;
} catch (Exception e) {
System.out.println("-----------Error:文档转换失败:" + e.getMessage() +"\n====================");
return false;
} finally {
ComThread.Release();
}
}
// excel转换为pdf
public static boolean excel2PDF(String inputFile, String pdfFile) {
System.out.println("\n====================\n启动Excel...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch docs = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
System.out.println("打开文档..." + inputFile);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
true).toDispatch();
System.out.println("转换文档到PDF..." + pdfFile);
Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);

Dispatch.call(excel, "Close", false);
app.invoke("Quit");

long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.\n====================");

return true;
} catch (Exception e) {
System.out.println("========Error:文档转换失败:=========");
System.out.println("(若找不到要打印的任何内容,可能是因为文件无内容)");
System.out.println(e.getMessage()+"\n====================");
return false;
} finally {
ComThread.Release();
}
}
// ppt转换为pdf
public static boolean ppt2PDF(String inputFile, String pdfFile) {
System.out.println("\n====================\n启动PPT...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch ppts = null;
Dispatch ppt = null;
try {
app = new ActiveXComponent("PowerPoint.Application");
// app.setProperty("Visible", msofalse);
System.out.println("打开文档..." + inputFile);
ppts = app.getProperty("Presentations").toDispatch();

ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();
System.out.println("转换文档到PDF..." + pdfFile);
Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);

Dispatch.call(ppt, "Close");

long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
app.invoke("Quit");
return true;
} catch (Exception e) {
System.out.println("========Error:文档转换失败:=========");
System.out.println("若发生PowerPoint 存储此文件时发生错误,可能是文件无内容");
System.out.println(e.getMessage()+"\n====================");
return false;
} finally {
ComThread.Release();
}
}
}
190 changes: 190 additions & 0 deletions Office2PDF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
"""
【程序功能】:将【目标文件夹】内所有的 ppt、excel、word 均生成一份对应的 PDF 文件
【作者】:evgo,公众号(随风前行),Github(evgo2017)
【目标文件夹】:默认为此程序目前所在的文件夹;
若输入路径,则为该文件夹(只转换该层,不转换子文件夹下内容)
【生成的pdf名称】:原始名称+.pdf
"""
import os, win32com.client, gc

# Word
def word2Pdf(filePath, words):
# 如果没有文件则提示后直接退出
if(len(words)<1):
print ("\n【无 Word 文件】\n")
return
# 开始转换
print ("\n【开始 Word -> PDF 转换】")
try:
print ("打开 Word 进程...")
word = win32com.client.Dispatch("Word.Application")
word.Visible = 0
word.DisplayAlerts = False
doc = None
for i in range(len(words)):
print(i)
fileName = words[i] # 文件名称
fromFile = os.path.join(filePath, fileName) # 文件地址
toFileName = changeSufix2Pdf(fileName) # 生成的文件名称
toFile = toFileJoin(filePath,toFileName) # 生成的文件地址

print ("转换:"+fileName+"文件中...")
# 某文件出错不影响其他文件打印
try:
doc = word.Documents.Open(fromFile)
doc.SaveAs(toFile,17) # 生成的所有 PDF 都会在 PDF 文件夹中
print ("转换到:"+toFileName+"完成")
except Exception as e:
print(e)
# 关闭 Word 进程
print ("所有 Word 文件已打印完毕")
print ("结束 Word 进程...\n")
doc.Close()
doc = None
word.Quit()
word = None
except Exception as e:
print(e)
finally:
gc.collect()

# Excel
def excel2Pdf(filePath, excels):
# 如果没有文件则提示后直接退出
if(len(excels)<1):
print ("\n【无 Excel 文件】\n")
return
# 开始转换
print ("\n【开始 Excel -> PDF 转换】")
try:
print ("打开 Excel 进程中...")
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = 0
excel.DisplayAlerts = False
wb = None
ws = None
for i in range(len(excels)):
print(i)
fileName = excels[i] # 文件名称
fromFile = os.path.join(filePath, fileName) # 文件地址

print ("转换:"+fileName+"文件中...")
# 某文件出错不影响其他文件打印
try:
wb = excel.Workbooks.Open(fromFile)
for j in range(wb.Worksheets.Count): # 工作表数量,一个工作簿可能有多张工作表
toFileName = addWorksheetsOrder(fileName, j+1) # 生成的文件名称
toFile = toFileJoin(filePath,toFileName) # 生成的文件地址

ws = wb.Worksheets(j+1) # 若为[0]则打包后会提示越界
ws.ExportAsFixedFormat(0,toFile) # 每一张都需要打印
print ("转换至:"+toFileName+"文件完成")
except Exception as e:
print(e)
# 关闭 Excel 进程
print ("所有 Excel 文件已打印完毕")
print ("结束 Excel 进程中...\n")
ws = None
wb.Close()
wb = None
excel.Quit()
excel = None
except Exception as e:
print(e)
finally:
gc.collect()

# PPT
def ppt2Pdf(filePath, ppts):
# 如果没有文件则提示后直接退出
if(len(ppts)<1):
print ("\n【无 PPT 文件】\n")
return
# 开始转换
print ("\n【开始 PPT -> PDF 转换】")
try:
print ("打开 PowerPoint 进程中...")
powerpoint = win32com.client.Dispatch("PowerPoint.Application")
ppt = None
# 某文件出错不影响其他文件打印

for i in range(len(ppts)):
print(i)
fileName = ppts[i] # 文件名称
fromFile = os.path.join(filePath, fileName) # 文件地址
toFileName = changeSufix2Pdf(fileName) # 生成的文件名称
toFile = toFileJoin(filePath,toFileName) # 生成的文件地址

print ("转换:"+fileName+"文件中...")
try:
ppt = powerpoint.Presentations.Open(fromFile,WithWindow=False)
if ppt.Slides.Count>0:
ppt.SaveAs(toFile, 32) # 如果为空则会跳出提示框(暂时没有找到消除办法)
print ("转换至:"+toFileName+"文件完成")
else:
print("(错误,发生意外:此文件为空,跳过此文件)")
except Exception as e:
print(e)
# 关闭 PPT 进程
print ("所有 PPT 文件已打印完毕")
print ("结束 PowerPoint 进程中...\n")
ppt.Close()
ppt = None
powerpoint.Quit()
powerpoint = None
except Exception as e:
print(e)
finally:
gc.collect()

# 修改后缀名
def changeSufix2Pdf(file):
return file[:file.rfind('.')]+".pdf"
# 添加工作簿序号
def addWorksheetsOrder(file, i):
return file[:file.rfind('.')]+"_工作表"+str(i)+".pdf"
# 转换地址
def toFileJoin(filePath,file):
return os.path.join(filePath,'pdf',file[:file.rfind('.')]+".pdf")



# 开始程序
print ("====================程序开始====================")
print ("【程序功能】将目标路径下内所有的 ppt、excel、word 均生成一份对应的 PDF 文件,存在新生成的 pdf 文件夹中(需已经安装office,不包括子文件夹)")
print ("【作者】:evgo,公众号(随风前行),Github(evgo2017)\n")
print ("注意:若某 PPT 和 Excel 文件为空,则会出错跳过此文件。若转换 PPT 时间过长,请查看是否有报错窗口等待确认,暂时无法彻底解决 PPT 的窗口问题(为空错误已解决)。在关闭进程过程中,时间可能会较长,十秒左右,请耐心等待。")
filePath = input ("输入目标路径:(若为当前路径:"+os.getcwd()+",请直接回车)\n")

# 目标路径,若没有输入路径则为当前路径
if(filePath==""):
filePath = os.getcwd()

# 将目标文件夹所有文件归类,转换时只打开一个进程
words = []
ppts = []
excels = []

for fn in os.listdir(filePath):
if fn.endswith(('.doc', 'docx')):
words.append(fn)
if fn.endswith(('.ppt', 'pptx')):
ppts.append(fn)
if fn.endswith(('.xls', 'xlsx')):
excels.append(fn)

# 调用方法
print ("====================开始转换====================")

# 新建 pdf 文件夹,所有生成的 PDF 文件都放在里面
folder = filePath + '\\pdf\\'
if not os.path.exists(folder):
os.makedirs(folder)

word2Pdf(filePath,words)
excel2Pdf(filePath,excels)
ppt2Pdf(filePath,ppts)
print ("====================转换结束====================")
print ("\n====================程序结束====================")
os.system("pause")

Loading

0 comments on commit c5ead2c

Please sign in to comment.