In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "Python how to convert Office files to PDF". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Install Win32com
Before actual combat, you need to install Python win32com, the detailed installation steps are as follows:
Install pip install pywin32 with pip command
If we encounter an installation error, we can install it by updating the cloud with python -m pip install --upgrade pip:
python -m pip install --upgrade pip download offline install package install
If the pip command is not installed successfully, you can also download the offline package installation, the method steps are as follows: First, select the corresponding Python version on the official website to download the offline package: sourceforge.net/projects/pywin32/files/pywin32/Build%20221/ Download and install it foolishly.
file conversion logic
The detailed codes are as follows:
class PDFConverter: def __init__(self, pathname, export='. '): self._ handle_postfix = ['doc',' docx','ppt', 'pptx', 'xls', 'xlsx']#File types supported for conversion self._ filename_list = list() #List files self._ export_folder = os.path.join(os.path.abspath('. '), 'file_server/pdfconver') if not os.path.exists(self._ export_folder): os.mkdir(self._ export_folder) self._ enumerate_filename(pathname) def _enumerate_filename(self, pathname): ''' Read all file names ''' full_pathname = os.path.abspath(pathname) if os.path.isfile(full_pathname): if self._ is_legal_postfix(full_pathname): self._ filename_list.append(full_pathname) else: raise TypeError ('file {} suffix name illegal! Only the following file types are supported: {}. '.format(pathname, '、'.join(self._ handle_postfix))) elif os.path.isdir(full_pathname): for relpath, _, files in os.walk(full_pathname): for name in files: filename = os.path.join(full_pathname, relpath, name) if self._ is_legal_postfix(filename): self._ filename_list.append(os.path.join(filename)) else: The file/folder {} does not exist or is invalid. '.format(pathname)) def _is_legal_postfix(self, filename): return filename.split('. ')[-1].lower() in self._ handle_postfix and not os.path.basename(filename).startswith( '~') def run_conver(self): print ('Number of files to convert is: ', len(self._ filename_list)) for filename in self._ filename_list: postfix = filename.split('. ')[-1].lower() funcCall = getattr(self, postfix) print ('original file', filename) funcCall(filename) print ('Conversion complete! ')doc/docx to PDF
The doc/docx conversion to PDF code is as follows:
def doc(self, filename): name = os.path.basename(filename).split('. ')[0] + '.pdf' exportfile = os.path.join(self._ export_folder, name) print ('Save PDF file:', exportfile) gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) pythoncom.CoInitialize() w = Dispatch("Word.Application") pythoncom.CoInitialize() #plus prevent CoInitialize from not loading doc = w.Documents.Open(filename) doc.ExportAsFixedFormat(exportfile, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup, CreateBookmarks=constants.wdExportCreateHeadingBookmarks) w.Quit(constants.wdDoNotSaveChanges) def docx(self, filename): self.doc (filename)ppt/pptx to PDF
The ppt/pptx conversion to PDF code is as follows:
def ppt(self, filename): name = os.path.basename(filename).split('. ')[0] + '.pdf' exportfile = os.path.join(self._ export_folder, name) gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) pythoncom.CoInitialize() p = Dispatch("PowerPoint.Application") pythoncom.CoInitialize() ppt = p.Presentations.Open(filename, False, False, False) ppt.ExportAsFixedFormat(exportfile, 2, PrintRange=None) print ('Save PDF file:', exportfile) p.Quit() def pptx(self, filename): self.ppt(filename)xls/xlsx to PDF def xls(self, filename): name = os.path.basename(filename).split('. ')[0] + '.pdf' exportfile = os.path.join(self._ export_folder, name) pythoncom.CoInitialize() xlApp = DispatchEx("Excel.Application") pythoncom.CoInitialize() xlApp.Visible = False xlApp.DisplayAlerts = 0 books = xlApp.Workbooks.Open(filename, False) books.ExportAsFixedFormat(0, exportfile) books.Close(False) print ('Save PDF file:', exportfile) xlApp.Quit() def xlsx(self, filename): self.xls(filename) performs conversion logic if __name__= "__main__": #Support folder batch import #folder = 'tmp' #pathname = os.path.join(os.path.abspath('. '), folder) #also supports conversion of individual files pathname = "G:/python_study/test.doc" pdfConverter = PDFConverter(pathname) pdfConverter.run_conver()"Python how to convert Office files to PDF" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.