python mac docx格式转pdf
1.翻了很多资料发现大多是Windows的库偏多,并且不支持linux,还需要借助word软件,方案是首先docx 转 html 再转 pdf
代码:
from pydocx import PyDocX from xhtml2pdf import pisa import os def word2html(inputFile): with open(os.path.dirname(__file__) + '/' + inputFile.name, "wb") as f: for chunk in inputFile.chunks(): f.write(chunk) file_path = os.path.dirname(__file__) + '/' file_input_path = file_path + inputFile.name html = PyDocX.to_html(file_input_path) outputFile = file_path + (inputFile.name.replace('.docx', '.html')) f = open(outputFile, 'w', encoding="utf-8") f.write(html) f.close() return outputFile def convertHtmlToPdf(sourceHtml, outputFilename): data = open(sourceHtml).read() resultFile = open(outputFilename, "w+b") pisaStatus = pisa.CreatePDF( data, dest=resultFile) resultFile.close() return resultFile def remove_resume_file(resume_file): for root, dirs, files in os.walk(os.path.dirname(__file__)): for name in files: if name.endswith(f"{resume_file}.docx") or name.endswith(f"{resume_file}.html") or name.endswith(f"{resume_file}.pdf"): os.remove(os.path.join(root, name))
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)