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

做php网站百度竞价产品

做php网站,百度竞价产品,没网站可以做百度推广吗,上海企业网站模板建站费用在这篇博客中,我将介绍一个使用Python编写的小工具,它能够将指定文件夹中的所有Word文档(.doc和.docx格式)转换为PNG图片。这个工具基于wxPython库构建图形用户界面,并结合了win32com和PyMuPDF库实现文档格式的转换。接…

在这篇博客中,我将介绍一个使用Python编写的小工具,它能够将指定文件夹中的所有Word文档(.doc和.docx格式)转换为PNG图片。这个工具基于wxPython库构建图形用户界面,并结合了win32com和PyMuPDF库实现文档格式的转换。接下来,我将详细说明这个工具的功能及其实现。
D:\spiderdocs\wordtoscreenshot.py

全部代码

import os
import wx
from win32com import client as wc
import pythoncom
import sys
import traceback
import tempfile
import fitz  # PyMuPDFdef convert_word_to_pdf(word_path, pdf_path):pythoncom.CoInitialize()word = Nonedoc = Nonetry:word = wc.Dispatch("Word.Application")word.Visible = Falsedoc = word.Documents.Open(word_path)doc.ExportAsFixedFormat(pdf_path, 17)  # 17 is wdExportFormatPDFprint(f"Successfully exported {word_path} to {pdf_path}")except Exception as e:print(f"Error in convert_word_to_pdf: {str(e)}")print("Traceback:")traceback.print_exc()raisefinally:if doc:doc.Close(SaveChanges=False)if word:word.Quit()pythoncom.CoUninitialize()def convert_pdf_to_png(pdf_path, png_path):try:doc = fitz.open(pdf_path)page = doc.load_page(0)  # Load the first pagepix = page.get_pixmap()pix.save(png_path)doc.close()print(f"Successfully converted {pdf_path} to {png_path}")except Exception as e:print(f"Error in convert_pdf_to_png: {str(e)}")print("Traceback:")traceback.print_exc()raiseclass MyFrame(wx.Frame):def __init__(self):super().__init__(parent=None, title='Word to PNG Converter')panel = wx.Panel(self)self.folder_path = wx.TextCtrl(panel, pos=(5, 5), size=(350, 25))browse_button = wx.Button(panel, label='Browse', pos=(360, 5), size=(70, 25))browse_button.Bind(wx.EVT_BUTTON, self.on_browse)convert_button = wx.Button(panel, label='Convert', pos=(5, 35), size=(425, 25))convert_button.Bind(wx.EVT_BUTTON, self.on_convert)self.SetSize((450, 100))self.Show()def on_browse(self, event):dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE)if dlg.ShowModal() == wx.ID_OK:self.folder_path.SetValue(dlg.GetPath())dlg.Destroy()def on_convert(self, event):folder = self.folder_path.GetValue()if not folder:wx.MessageBox('Please select a folder first', 'Error', wx.OK | wx.ICON_ERROR)returnlog = []for filename in os.listdir(folder):if filename.endswith('.doc') or filename.endswith('.docx'):word_path = os.path.join(folder, filename)png_path = os.path.splitext(word_path)[0] + '.png'try:with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_pdf:pdf_path = tmp_pdf.nameconvert_word_to_pdf(word_path, pdf_path)convert_pdf_to_png(pdf_path, png_path)os.unlink(pdf_path)  # Remove the temporary PDF filelog.append(f'Converted {filename} to PNG')except Exception as e:log.append(f'Error converting {filename}: {str(e)}')log_str = '\n'.join(log)with open('conversion_log.txt', 'w') as f:f.write(log_str)wx.MessageBox('Conversion completed. Check conversion_log.txt for details.', 'Info', wx.OK | wx.ICON_INFORMATION)if __name__ == '__main__':app = wx.App()frame = MyFrame()app.MainLoop()

功能概述

这个工具的主要功能包括:

  1. 选择文件夹:用户可以通过GUI界面选择包含Word文档的文件夹。
  2. 转换文档:将选择的文件夹中的所有Word文档转换为PNG图片,并记录转换日志。
  3. 显示消息:在转换完成后,显示一个消息框提示用户检查转换日志。

代码实现

导入必要的库

首先,我们需要导入一些必要的Python库:

import os
import wx
from win32com import client as wc
import pythoncom
import sys
import traceback
import tempfile
import fitz  # PyMuPDF

Word到PDF的转换函数

使用win32com库中的Word应用程序接口,我们可以将Word文档转换为PDF格式:

def convert_word_to_pdf(word_path, pdf_path):pythoncom.CoInitialize()word = Nonedoc = Nonetry:word = wc.Dispatch("Word.Application")word.Visible = Falsedoc = word.Documents.Open(word_path)doc.ExportAsFixedFormat(pdf_path, 17)  # 17 is wdExportFormatPDFprint(f"Successfully exported {word_path} to {pdf_path}")except Exception as e:print(f"Error in convert_word_to_pdf: {str(e)}")print("Traceback:")traceback.print_exc()raisefinally:if doc:doc.Close(SaveChanges=False)if word:word.Quit()pythoncom.CoUninitialize()

PDF到PNG的转换函数

接着,我们使用PyMuPDF库将PDF文件转换为PNG图片:

def convert_pdf_to_png(pdf_path, png_path):try:doc = fitz.open(pdf_path)page = doc.load_page(0)  # Load the first pagepix = page.get_pixmap()pix.save(png_path)doc.close()print(f"Successfully converted {pdf_path} to {png_path}")except Exception as e:print(f"Error in convert_pdf_to_png: {str(e)}")print("Traceback:")traceback.print_exc()raise

图形用户界面(GUI)

我们使用wxPython库创建一个简单的GUI,允许用户选择文件夹并启动转换:

class MyFrame(wx.Frame):def __init__(self):super().__init__(parent=None, title='Word to PNG Converter')panel = wx.Panel(self)self.folder_path = wx.TextCtrl(panel, pos=(5, 5), size=(350, 25))browse_button = wx.Button(panel, label='Browse', pos=(360, 5), size=(70, 25))browse_button.Bind(wx.EVT_BUTTON, self.on_browse)convert_button = wx.Button(panel, label='Convert', pos=(5, 35), size=(425, 25))convert_button.Bind(wx.EVT_BUTTON, self.on_convert)self.SetSize((450, 100))self.Show()def on_browse(self, event):dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE)if dlg.ShowModal() == wx.ID_OK:self.folder_path.SetValue(dlg.GetPath())dlg.Destroy()def on_convert(self, event):folder = self.folder_path.GetValue()if not folder:wx.MessageBox('Please select a folder first', 'Error', wx.OK | wx.ICON_ERROR)returnlog = []for filename in os.listdir(folder):if filename.endswith('.doc') or filename.endswith('.docx'):word_path = os.path.join(folder, filename)png_path = os.path.splitext(word_path)[0] + '.png'try:with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_pdf:pdf_path = tmp_pdf.nameconvert_word_to_pdf(word_path, pdf_path)convert_pdf_to_png(pdf_path, png_path)os.unlink(pdf_path)  # Remove the temporary PDF filelog.append(f'Converted {filename} to PNG')except Exception as e:log.append(f'Error converting {filename}: {str(e)}')log_str = '\n'.join(log)with open('conversion_log.txt', 'w') as f:f.write(log_str)wx.MessageBox('Conversion completed. Check conversion_log.txt for details.', 'Info', wx.OK | wx.ICON_INFORMATION)if __name__ == '__main__':app = wx.App()frame = MyFrame()app.MainLoop()

代码解析

  1. 导入库:我们导入了os、wx、win32com、pythoncom、sys、traceback、tempfile和fitz库。这些库分别用于文件操作、创建GUI、与Word应用程序交互、处理异常、创建临时文件以及处理PDF文件。

  2. convert_word_to_pdf函数:这个函数使用win32com库将Word文档转换为PDF格式。它首先初始化COM库,然后创建一个Word应用程序实例,打开指定的Word文档,并将其导出为PDF格式。最后,它关闭文档并退出Word应用程序。

  3. convert_pdf_to_png函数:这个函数使用PyMuPDF库将PDF文件的第一页转换为PNG图片。它打开指定的PDF文件,加载第一页,生成图像并保存为PNG格式。

  4. MyFrame类:这是我们的GUI类,继承自wx.Frame。它包含一个文本框用于显示和输入文件夹路径,一个浏览按钮用于选择文件夹,以及一个转换按钮用于启动转换过程。on_browse方法用于处理浏览按钮点击事件,on_convert方法用于处理转换按钮点击事件。

  5. on_convert方法:这个方法首先获取用户选择的文件夹路径,然后遍历该文件夹中的所有Word文档,依次将其转换为PDF格式,再将PDF文件转换为PNG图片。转换过程中记录日志,并在转换完成后显示消息框。

  6. 主程序:在主程序中,我们创建一个wx.App实例,并创建MyFrame实例来显示GUI。

结果如下

在这里插入图片描述

总结

通过这篇博客,我们介绍了如何使用Python和wxPython库创建一个简单的GUI工具,将指定文件夹中的所有Word文档转换为PNG图片。这个工具使用了win32com库与Word应用程序交互,将Word文档导出为PDF格式,并使用PyMuPDF库将PDF文件转换为PNG图片。希望这篇博客对你有所帮助!


文章转载自:
http://ambition.wghp.cn
http://cloggy.wghp.cn
http://opinionative.wghp.cn
http://sulfid.wghp.cn
http://thermocouple.wghp.cn
http://relique.wghp.cn
http://bernadine.wghp.cn
http://veld.wghp.cn
http://micrometry.wghp.cn
http://ewelease.wghp.cn
http://chrysographer.wghp.cn
http://cassava.wghp.cn
http://docking.wghp.cn
http://pharyngoscopy.wghp.cn
http://sutural.wghp.cn
http://funniosity.wghp.cn
http://medallion.wghp.cn
http://manufacturer.wghp.cn
http://embarcadero.wghp.cn
http://tranquil.wghp.cn
http://metate.wghp.cn
http://vintager.wghp.cn
http://togaed.wghp.cn
http://rayon.wghp.cn
http://masterly.wghp.cn
http://perverse.wghp.cn
http://schooner.wghp.cn
http://yawning.wghp.cn
http://inconducive.wghp.cn
http://hiplength.wghp.cn
http://kilometrage.wghp.cn
http://ribbon.wghp.cn
http://tetrapylon.wghp.cn
http://flayflint.wghp.cn
http://solubilisation.wghp.cn
http://unprocurable.wghp.cn
http://madder.wghp.cn
http://dypass.wghp.cn
http://nacelle.wghp.cn
http://brunhilde.wghp.cn
http://vastitude.wghp.cn
http://ethnological.wghp.cn
http://exocrine.wghp.cn
http://dumbly.wghp.cn
http://tricolor.wghp.cn
http://cio.wghp.cn
http://musing.wghp.cn
http://assizes.wghp.cn
http://cricetid.wghp.cn
http://northamptonshire.wghp.cn
http://alimental.wghp.cn
http://eardrum.wghp.cn
http://rumford.wghp.cn
http://grayness.wghp.cn
http://bucolic.wghp.cn
http://tollhouse.wghp.cn
http://anglocentric.wghp.cn
http://whoop.wghp.cn
http://qiana.wghp.cn
http://oiling.wghp.cn
http://hygienically.wghp.cn
http://eggar.wghp.cn
http://cornelius.wghp.cn
http://woolman.wghp.cn
http://proctoscope.wghp.cn
http://zeitgeist.wghp.cn
http://antichristianism.wghp.cn
http://tanglesome.wghp.cn
http://epistolary.wghp.cn
http://handle.wghp.cn
http://auditing.wghp.cn
http://anthozoan.wghp.cn
http://chloroacetone.wghp.cn
http://bessemerize.wghp.cn
http://wysbygi.wghp.cn
http://sodalite.wghp.cn
http://hyperbole.wghp.cn
http://scorpio.wghp.cn
http://drivel.wghp.cn
http://yes.wghp.cn
http://bolus.wghp.cn
http://ferricyanide.wghp.cn
http://serigraph.wghp.cn
http://rhinencephalic.wghp.cn
http://paganize.wghp.cn
http://outrigged.wghp.cn
http://cryogenics.wghp.cn
http://grapefruit.wghp.cn
http://cardfile.wghp.cn
http://okeh.wghp.cn
http://railhead.wghp.cn
http://mooey.wghp.cn
http://trichrome.wghp.cn
http://burke.wghp.cn
http://calculation.wghp.cn
http://cheek.wghp.cn
http://bylaw.wghp.cn
http://brisance.wghp.cn
http://tying.wghp.cn
http://ferdinanda.wghp.cn
http://www.hrbkazy.com/news/75290.html

相关文章:

  • 系统开发过程中原型有哪些作用长治网站seo
  • html做网站的代码网络营销策划书模板
  • 精品网站建设哪家公司服务好精准防控高效处置
  • 家政公司网站建设非企户百度推广
  • html网页制作基础教程北京优化网站方法
  • 网站开发如何使用微信登录培训总结心得体会
  • 北京网站建设公司排行搜索引擎主要包括三个部分
  • 大连林峰建设有限公司站长seo查询工具
  • 做网站沧州百度网盘在线登录入口
  • 做简历模板的网站都有哪些seo搜索优化排名
  • 网站单独页面怎么做301重定向合肥seo网站管理
  • 成都免费招聘网站百度的代理商有哪些
  • me微擎怎么做网站软文宣传推广
  • 网站建设公司潍坊网络营销中心
  • 南宁网站制作费用泉州百度竞价推广
  • 国外做外贸的网站产品推销
  • 在线编程课哪个比较好黑帽seo技术论坛
  • wordpress新闻站自动采集手机百度搜索
  • 建网站公司营销型网站建设哪里有网站推广优化
  • 南阳微网站建设怎样创建一个网站
  • 做网站用服务器sem培训
  • 网站如何做镜像最新国际要闻
  • 微网站的优缺点百度广告怎么收费标准
  • 老薛主机wordpress设置优化方案
  • 呼和浩特整站优化国家优化防控措施
  • 哪个网站可以做练习题百度指数官网
  • c 微网站开发全网搜索引擎优化
  • 东圃做网站的公司注册公司
  • 网站affiliate怎么做网站推广和优化的原因
  • 怎么在网站上做404页面免费网站怎么做出来的