Hướng dẫn how do i save a python file as a pdf? - làm cách nào để lưu tệp python dưới dạng pdf?
Bạn nên sử dụng 6 trong trường hợp này:
Từ tài liệu:
Vì vậy, điều đó có nghĩa là: 7 Trả về đầu ra dưới dạng đối tượng chuỗi, hãy sử dụng nó khi bạn tải xuống một tệp văn bản. Chẳng hạn như tệp HTML, v.v.text file. Such as HTML file, etc.Và 6 Trả về đầu ra dưới dạng đối tượng byte, hãy sử dụng nó khi bạn tải xuống một tệp nhị phân. Chẳng hạn như tệp PDF, tệp âm thanh, hình ảnh, v.v.binary file. Such as PDF file, audio file, image, etc.Bạn cũng có thể sử dụng 9 thay thế. Tuy nhiên, sử dụng nó khi tệp mà bạn sắp tải xuống là lớn. Dưới đây là một ví dụ cơ bản mà bạn cũng có thể tìm thấy trong tài liệu:
def save_as_pdf(png_path): assert png_path.endswith('.png') pdf_path = re.sub('.png$', '.pdf', png_path) print "Writing: %s"%pdf_path plotter.plt.savefig(pdf_path) return0 là kích thước chunk mà bạn muốn sử dụng. Nếu bạn đặt nó thành def save_as_pdf(png_path): assert png_path.endswith('.png') pdf_path = re.sub('.png$', '.pdf', png_path) print "Writing: %s"%pdf_path plotter.plt.savefig(pdf_path) return1, thì các yêu cầu sẽ tải xuống tệp đó byte def save_as_pdf(png_path): assert png_path.endswith('.png') pdf_path = re.sub('.png$', '.pdf', png_path) print "Writing: %s"%pdf_path plotter.plt.savefig(pdf_path) return1 đầu tiên, hãy ghi chúng vào tệp và thực hiện hết lần này đến lần khác, trừ khi nó hoàn thành. Vì vậy, điều này có thể lưu RAM của bạn. Nhưng tôi thích sử dụng 6 thay thế trong trường hợp này vì tệp của bạn nhỏ. Như bạn có thể thấy sử dụng 9 rất phức tạp.Liên quan:
23 ví dụ mã Python được tìm thấy liên quan đến "Lưu PDF". Bạn có thể bỏ phiếu cho những người bạn thích hoặc bỏ phiếu cho những người bạn không thích và đi đến dự án gốc hoặc tệp nguồn bằng cách theo các liên kết trên mỗi ví dụ. save pdf". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. ví dụ 1 def save_as_pdf(png_path): assert png_path.endswith('.png') pdf_path = re.sub('.png$', '.pdf', png_path) print "Writing: %s"%pdf_path plotter.plt.savefig(pdf_path) return Ví dụ 2 def save_as_pdf(self, results, url): tmpdir = mkdtemp() try: options = {"load-error-handling": "ignore"} pdfkit.from_url(url, path.join(tmpdir, 'out.pdf'), options=options) with open(path.join(tmpdir, 'out.pdf'), 'rb') as pdf: pdf_import = AttachedFile.from_content( pdf, 'import.pdf', 'application/pdf') results.investigation.update(import_document=pdf_import) except Exception as e: print(e) rmtree(tmpdir) Ví dụ 3 def save_pdf(full_dir,filename,i): if not os.path.exists(full_dir): os.makedirs(full_dir) # Open the URL and retrieve data file_loc = full_dir + filename if not os.path.exists(file_loc) or force == True: if i.startswith("//"): i = "http:" + i print "Downloading : " + i web = urllib.urlopen(i) print "Saving to : " + file_loc # Save Data to disk output = open(file_loc,'wb') output.write(web.read()) output.close() else: print "Skipping " + i + " - file exists or is a dated API document, use './getAWSdocs.py --force' to force override" Ví dụ 4 def save_pdf_binary_string( self, pdf_string: bytes, out_filename: str ) -> pathlib.Path: """ Save the binary string to the store using the out_filename Parameters ---------- pdf_string : str String representing a pdf in binary format out_filename: str The name of the pdf file that will stored Returns ------- None """ pdf_filename = self.store_path.joinpath(out_filename) with open(pdf_filename, "wb") as fp: fp.write(pdf_string) return pdf_filename Ví dụ 5 def save_pdf(self, response): """ Save pdf files """ path = response.url.split('/')[-1] self.logger.info('Saving PDF %s', path); with open(path, 'wb') as file: file.write(response.body); Ví dụ 6 def save_pdf(self, file_name='graph.pdf', prog='dot'): """Draw the graph and save as an image or pdf file. Parameters ---------- file_name : Optional[str] The name of the file to save the graph as. Default: graph.pdf prog : Optional[str] The graphviz program to use for graph layout. Default: dot """ self.graph.draw(file_name, prog=prog) Ví dụ 7 def save_pdf(self): """save the writer to a pdf file with name 'name_new.pdf' """ if os.path.exists(self._new_path): os.remove(self._new_path) with open(self._new_path, 'wb') as out: self.writer.write(out) return self._new_path Ví dụ 8 0Ví dụ 9 1Ví dụ 10 2Ví dụ 11 3Ví dụ 12 4Ví dụ 13 5Ví dụ 14 6Ví dụ 15 7Ví dụ 16 8Ví dụ 17 9Ví dụ 18 0Ví dụ 19 1Ví dụ 20 2Ví dụ 21 3Ví dụ 22
4Ví dụ 23 5 |