当前位置: 首页 > news >正文

微信网站开发 全屏最新的网站开发框架

微信网站开发 全屏,最新的网站开发框架,辽宁住房和城乡建设厅网站,大连中小网站建设公司今天看见了一个有意思的脚本Python批量实现Word、EXCLE、PPT转PDF文件。 因为我平时word用的比较的多,所以深有体会,具体怎么实现的我们就不讨论了,因为这个去学了也没什么提升,不然也不会当作脚本了。这里我将其放入了pyzjr库中…

今天看见了一个有意思的脚本Python批量实现Word、EXCLE、PPT转PDF文件。

因为我平时word用的比较的多,所以深有体会,具体怎么实现的我们就不讨论了,因为这个去学了也没什么提升,不然也不会当作脚本了。这里我将其放入了pyzjr库中,也方便大家进行调用。

你可以去下载pyzjr:

pip install pyzjr -i https://pypi.tuna.tsinghua.edu.cn/simple

调用方法:

import pyzjr as pz# 实例化对象
Mpdf = pz.Microsoft2PDF()
# 调用类的方法
Mpdf.Word2Pdf()  # word -> pdf
Mpdf.Excel2Pdf()  # excel -> pdf
Mpdf.PPt2Pdf()  # ppt -> pdf
Mpdf.WEP2Pdf()  # word,excel,ppt -> pdf

上面就是api的调用了,统一会将文件存放在目标文件夹下新建的名为pdf文件夹中。

pyzjr中的源码:

import win32com.client, gc, osclass Microsoft2PDF():"""Convert Microsoft Office documents (Word, Excel, PowerPoint) to PDF format"""def __init__(self,filePath = ""):""":param filePath: 如果默认是空字符,就默认当前路径"""self.flagW = self.flagE = self.flagP = 1self.words = []self.ppts = []self.excels = []if filePath == "":filePath = os.getcwd()folder = filePath + '\\pdf\\'self.folder = CreateFolder(folder,debug=False)self.filePath = filePathfor i in os.listdir(self.filePath):if i.endswith(('.doc', 'docx')):self.words.append(i)if i.endswith(('.ppt', 'pptx')):self.ppts.append(i)if i.endswith(('.xls', 'xlsx')):self.excels.append(i)if len(self.words) < 1:print("\n[pyzjr]:No Word files\n")self.flagW = 0if len(self.ppts) < 1:print("\n[pyzjr]:No PPT file\n")self.flagE = 0if len(self.excels) < 1:print("\n[pyzjr]:No Excel file\n")self.flagP = 0def Word2Pdf(self):if self.flagW == 0:return 0else:print("\n[Start Word ->PDF conversion]")try:print("Open Word Process...")word = win32com.client.Dispatch("Word.Application")word.Visible = 0word.DisplayAlerts = Falsedoc = Nonefor i in range(len(self.words)):print(i)fileName = self.words[i]  # file namefromFile = os.path.join(self.filePath, fileName)  # file addresstoFileName = self.changeSufix2Pdf(fileName)  # Generated file nametoFile = self.toFileJoin(toFileName)  # Generated file addressprint("Conversion:" + fileName + "in files...")try:doc = word.Documents.Open(fromFile)doc.SaveAs(toFile, 17)print("Convert to:" + toFileName + "file completion")except Exception as e:print(e)print("All Word files have been printed")print("End Word Process...\n")doc.Close()doc = Noneword.Quit()word = Noneexcept Exception as e:print(e)finally:gc.collect()def Excel2Pdf(self):if self.flagE == 0:return 0else:print("\n[Start Excel -> PDF conversion]")try:print("open Excel Process...")excel = win32com.client.Dispatch("Excel.Application")excel.Visible = 0excel.DisplayAlerts = Falsewb = Nonews = Nonefor i in range(len(self.excels)):print(i)fileName = self.excels[i]fromFile = os.path.join(self.filePath, fileName)print("Conversion:" + fileName + "in files...")try:wb = excel.Workbooks.Open(fromFile)for j in range(wb.Worksheets.Count):  # Number of worksheets, one workbook may have multiple worksheetstoFileName = self.addWorksheetsOrder(fileName, j + 1)toFile = self.toFileJoin(toFileName)ws = wb.Worksheets(j + 1)ws.ExportAsFixedFormat(0, toFile)print("Convert to:" + toFileName + "file completion")except Exception as e:print(e)# 关闭 Excel 进程print("All Excel files have been printed")print("Ending Excel process...\n")ws = Nonewb.Close()wb = Noneexcel.Quit()excel = Noneexcept Exception as e:print(e)finally:gc.collect()def PPt2Pdf(self):if self.flagP == 0:return 0else:print("\n[Start PPT ->PDF conversion]")try:print("Opening PowerPoint process...")powerpoint = win32com.client.Dispatch("PowerPoint.Application")ppt = Nonefor i in range(len(self.ppts)):print(i)fileName = self.ppts[i]fromFile = os.path.join(self.filePath, fileName)toFileName = self.changeSufix2Pdf(fileName)toFile = self.toFileJoin(toFileName)print("Conversion:" + fileName + "in files...")try:ppt = powerpoint.Presentations.Open(fromFile, WithWindow=False)if ppt.Slides.Count > 0:ppt.SaveAs(toFile, 32)print("Convert to:" + toFileName + "file completion")else:print("Error, unexpected: This file is empty, skipping this file")except Exception as e:print(e)print("All PPT files have been printed")print("Ending PowerPoint process...\n")ppt.Close()ppt = Nonepowerpoint.Quit()powerpoint = Noneexcept Exception as e:print(e)finally:gc.collect()def WEP2Pdf(self):"""Word, Excel and PPt are all converted to PDF.If there are many files, it may take some time"""print("Convert Microsoft Three Musketeers to PDF")self.Word2Pdf()self.Excel2Pdf()self.PPt2Pdf()print(f"All files have been converted, you can find them in the {self.folder}")def changeSufix2Pdf(self,file):"""将文件后缀更改为.pdf"""return file[:file.rfind('.')] + ".pdf"def addWorksheetsOrder(self,file, i):"""在文件名中添加工作表顺序"""return file[:file.rfind('.')] + "_worksheet" + str(i) + ".pdf"def toFileJoin(self, file):"""将文件路径和文件名连接为完整的文件路径"""return os.path.join(self.filePath, 'pdf', file[:file.rfind('.')] + ".pdf")

 这里我对原先博主的代码进行了一定的优化,使其可供我们调用。

这是控制台打印出来的信息,我们可以发现在调用WEP2Pdf时,如果当前文件夹中没有word的文件也能继续去转换。 

http://www.yayakq.cn/news/934597/

相关文章:

  • 手机建设中网站首页小程序定制开发网站
  • 天津网站搜索排名做视频分享网站的参考书
  • 九江建企业网站菏泽网站建设服务
  • 网站后台开发深圳公司网站设计公司
  • 网站建设基本流程教学视频教程邢台快用网络科技有限公司
  • 无锡网站设计无锡网站建设做高端网站
  • 在线房屋建设设计网站品牌注册公司
  • 旅游网站建设的参考文献新手网络推广怎么干
  • 上海网站制作哪家好网站建设背景图片
  • 安阳企业建网站wordpress pcms
  • 定制化网站开发11网拍推广平台
  • 网站快照更新慢计算机专业是干什么的
  • 网站配色方案橙色专业网站设计发展前景
  • 网站开发的工作经验配资网站建设
  • 做博客网站怎么赚钱吗cms监控软件电脑版官方下载
  • 博客建站系统可以发外链的网站或平台有哪些
  • 网站建设衡水做网站推广的工作好吗
  • 威海做企业网站的公司做网站怎么收集资料
  • 哔哩哔哩网站4 3比例怎么做网站被别人域名绑定
  • 什么人需要网站建设淄博做网站的哪家最好
  • 网站 乱码阿里云做网站麻烦吗
  • 室内设计效果图360全景图抖音搜索排名优化
  • 查询类网站开发wordpress缓存删除了有什么后果
  • 怎么做网站底部文件建设网站需要了解些什么
  • 如何做网站后台管理系统洛阳设计公司官网
  • 中国联通网站备案管理系统哪些网站可以做店铺推广
  • 网站登录窗口怎么做世纪购网站开发招聘
  • 网站优化公司谷歌优化网站服务器速度
  • 微信企业微网站上海注册公司多久
  • 个人简历模板网站凡科建站骗子