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

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
6 trong trường hợp này:

with open('/tmp/metadata.pdf', 'wb') as f:
    f.write(response.content)

Từ tài liệu:

Bạn cũng có thể truy cập phần thân phản hồi dưới dạng byte, cho các yêu cầu không phải là văn bản:

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...

Vì vậy, điều đó có nghĩa là:

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
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.

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
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

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
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:

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

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 
0 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)
    return 
1, 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)
    return 
1 đầ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

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
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
import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
9 rất phức tạp.


Liên quan:

  • Làm thế nào để tải xuống tệp lớn trong python với yêu cầu.py?

  • Cách tải xuống hình ảnh bằng cách sử dụng các yêu cầu

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

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
0

Ví dụ 9

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
1

Ví dụ 10

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
2

Ví dụ 11

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
3

Ví dụ 12

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
4

Ví dụ 13

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
5

Ví dụ 14

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
6

Ví dụ 15

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
7

Ví dụ 16

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
8

Ví dụ 17

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
9

Ví dụ 18

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
0

Ví dụ 19

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
1

Ví dụ 20

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
2

Ví dụ 21

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
3

Ví dụ 22

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
4

Ví dụ 23

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)
5