全網(wǎng)最新用python實(shí)現(xiàn)各種文件類型轉(zhuǎn)換的方法
一、word轉(zhuǎn)pdf
先安裝win32庫(kù):pip install pywin32
from win32com.client import gencache from win32com.client import constants, gencache def createPdf(wordPath, pdfPath): """ word轉(zhuǎn)pdf :param wordPath: word文件路徑 :param pdfPath: 生成pdf文件路徑 """ word = gencache.EnsureDispatch('Word.Application') doc = word.Documents.Open(wordPath, ReadOnly=1) doc.ExportAsFixedFormat(pdfPath, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup, CreateBookmarks=constants.wdExportCreateHeadingBookmarks) word.Quit(constants.wdDoNotSaveChanges) createPdf('D:\桌面\論文閱讀筆記.docx','D:\桌面\論文閱讀筆記.pdf')
運(yùn)行結(jié)果:
二、excel轉(zhuǎn)pdf
# Import Module from win32com import client # Open Microsoft Excel excel = client.Dispatch("Excel.Application") # Read Excel File sheets = excel.Workbooks.Open('F:\書(shū)籍借閱信息.xlsx') work_sheets = sheets.Worksheets[0] # Convert into PDF File work_sheets.ExportAsFixedFormat(0, 'F:\書(shū)籍借閱信息.pdf') # 關(guān)閉服務(wù) excel.Quit()
運(yùn)行結(jié)果:
三、ppt轉(zhuǎn)pdf
# 1). 導(dǎo)入需要的模塊(打開(kāi)應(yīng)用程序的模塊) import win32com.client import os def ppt2pdf(filename, output_filename): """ PPT文件導(dǎo)出為pdf格式 :param filename: PPT文件的名稱 :param output_filename: 導(dǎo)出的pdf文件的名稱 :return: """ # 2). 打開(kāi)PPT程序 ppt_app = win32com.client.Dispatch('PowerPoint.Application') # ppt_app.Visible = True # 程序操作應(yīng)用程序的過(guò)程是否可視化 # 3). 通過(guò)PPT的應(yīng)用程序打開(kāi)指定的PPT文件 # filename = "C:/Users/Administrator/Desktop/PPT辦公自動(dòng)化/ppt/PPT素材1.pptx" # output_filename = "C:/Users/Administrator/Desktop/PPT辦公自動(dòng)化/ppt/PPT素材1.pdf" ppt = ppt_app.Presentations.Open(filename) # 4). 打開(kāi)的PPT另存為pdf文件。17數(shù)字是ppt轉(zhuǎn)圖片,32數(shù)字是ppt轉(zhuǎn)pdf。 ppt.SaveAs(output_filename, 32) print("導(dǎo)出成pdf格式成功!!!") # 退出PPT程序 ppt_app.Quit() # 要處理的目錄名稱 dirname = 'D:\桌面\智能算法設(shè)計(jì)與實(shí)現(xiàn)' # 列出指定目錄的內(nèi)容 filenames = os.listdir(dirname) # for循環(huán)依次訪問(wèn)指定目錄的所有文件名 for filename in filenames: # 判斷文件的類型,對(duì)所有的ppt文件進(jìn)行處理(ppt文件以ppt或者pptx結(jié)尾的) if filename.endswith('ppt') or filename.endswith('pptx'): # print(filename) # PPT素材1.pptx -> PPT素材1.pdf # 將filename以.進(jìn)行分割,返回2個(gè)信息,文件的名稱和文件的后綴名 base, ext = filename.split('.') # base=PPT素材1 ext=pdf new_name = base + '.pdf' # PPT素材1.pdf # ppt文件的完整位置: C:/Users/Administrator/Desktop/PPT辦公自動(dòng)化/ppt/PPT素材1.pptx filename = dirname + '/' + filename # pdf文件的完整位置: C:/Users/Administrator/Desktop/PPT辦公自動(dòng)化/ppt/PPT素材1.pdf output_filename = dirname + '/' + new_name # 將ppt轉(zhuǎn)成pdf文件 ppt2pdf(filename, output_filename)
運(yùn)行結(jié)果:
四、圖片轉(zhuǎn)pdf
from PIL import Image import os # 防止字符串亂碼 os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' def pic2pdf(img_path, pdf_path): file_list = os.listdir(img_path) for x in file_list: if "jpg" in x or 'png' in x or 'jpeg' in x: pdf_name = x.split('.')[0] im1 = Image.open(os.path.join(img_path, x)) im1.save(pdf_path + pdf_name + '.pdf', "PDF", resolution=100.0) if __name__ == '__main__': # 待轉(zhuǎn)換圖像路徑 img_path = r"D:\桌面\\" # 轉(zhuǎn)換后的pdf存放路徑 pdf_path = r'D:\桌面\\' pic2pdf(img_path=img_path, pdf_path=pdf_path)
五、pdf轉(zhuǎn)word
先安裝:pip install pdf2docx
from pdf2docx import Converter pdf_file = r'D:\桌面\論文閱讀筆記.pdf' docx_file = r'D:\桌面\論文閱讀筆記.docx' cv = Converter(pdf_file) cv.convert(docx_file, start=0, end=None) cv.close()
六、pdf轉(zhuǎn)圖片
先安裝:pip install pdf2image
from pdf2image import convert_from_path pages = convert_from_path('D:\桌面\論文閱讀筆記.pdf', 500) # 保存 for page in pages: page.save('D:\桌面\論文閱讀筆記.jpg', 'JPEG') # 多圖保存 for index, img in enumerate(pages): img.save('E:\識(shí)別\page_%s.jpg' % (index+1))
如果報(bào)錯(cuò)pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?
則需要安裝配置poppler:
Windows的poppler下載地址:http://blog.alivate.com.au/poppler-windows/
解壓縮后,將C:\Program Files\poppler-0.68.0\bin(可以放在其他位置)添加進(jìn)環(huán)境變量-系統(tǒng)變量-path中,重啟生效:
七、csv轉(zhuǎn)excel
import pandas as pd data = pd.read_csv('F:/train.csv',index_col=0) data.to_excel('F:/train.xlsx',encoding='utf-8')
八、excel轉(zhuǎn)csv
import pandas as pd data = pd.read_excel('F:/train.xlsx',index_col=0) data.to_csv('F:/train.csv',encoding='utf-8')
到此這篇關(guān)于全網(wǎng)最新用python實(shí)現(xiàn)各種文件類型轉(zhuǎn)換的方法的文章就介紹到這了,更多相關(guān)python文件類型轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)塊的網(wǎng)絡(luò)之VGG
雖然AlexNet證明深層神經(jīng)網(wǎng)絡(luò)卓有成效,但它沒(méi)有提供一個(gè)通用的模板來(lái)指導(dǎo)后續(xù)的研究人員設(shè)計(jì)新的網(wǎng)絡(luò)。下面,我們將介紹一些常用于設(shè)計(jì)深層神經(jīng)網(wǎng)絡(luò)的啟發(fā)式概念2021-10-10Python文件讀寫(xiě)處理日常任務(wù)終極工具實(shí)例
Python文件的讀寫(xiě)操作時(shí),有很多需要考慮的細(xì)節(jié),這包括文件打開(kāi)方式、讀取和寫(xiě)入數(shù)據(jù)的方法、異常處理等,在本文中,將深入探討Python中的文件操作,旨在提供全面的指南,幫你充分了解Python文件的讀寫(xiě)2023-11-11python Requsets下載開(kāi)源網(wǎng)站的代碼(帶索引 數(shù)據(jù))
這篇文章主要介紹了python Requsets下載開(kāi)源網(wǎng)站的代碼(帶索引 數(shù)據(jù)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04關(guān)于pytorch中網(wǎng)絡(luò)loss傳播和參數(shù)更新的理解
今天小編就為大家分享一篇關(guān)于pytorch中網(wǎng)絡(luò)loss傳播和參數(shù)更新的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python使用Marshmallow輕松實(shí)現(xiàn)序列化和反序列化
這篇文章主要為大家詳細(xì)介紹了Python如何使用Marshmallow輕松實(shí)現(xiàn)序列化和反序列化,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-03-03python連接mysql數(shù)據(jù)庫(kù)并讀取數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了python連接mysql數(shù)據(jù)庫(kù)并讀取數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09