Cara menggunakan BYTEARRAY pada Python

Cara Mengonversi PDF ke BYTEARRAY Menggunakan Python

Dengan perpustakaan Aspose.PDF untuk .NET Anda dapat mengonversi PDF menjadi BYTEARRAY secara terprogram. Perangkat lunak PDF dari Aspose sangat ideal untuk perorangan, bisnis kecil atau besar. Karena mampu memproses sejumlah besar informasi, lakukan konversi dengan cepat dan efisien dan lindungi data Anda. Fitur aneh dari Aspose.PDF adalah API untuk mengkonversi PDF ke BYTEARRAY. Sifat dari pendekatan ini adalah Anda hanya perlu membuka manajer paket nuget, mencari ‘Aspose.pdf untuk .NET’, dan menginstalnya tanpa pengaturan kompleks khusus. (Gunakan perintah dari Konsol Manajer Paket untuk menginstal). Untuk memverifikasi manfaat pustaka, coba gunakan cuplikan kode konversi PDF ke BYTEARRAY. Anda juga dapat menggunakan perintah berikut dari Konsol Manajer Paket:

Perintah Konsol Manajer Paket


PM> Instal-Paket Aspose.PDF

Langkah-langkah untuk Mengkonversi PDF ke BYTEARRAY melalui Python

Python for .NET pengembang dapat dengan mudah memuat & mengkonversi PDF file ke BYTEARRAY hanya dalam beberapa baris kode.

Persyaratan Sistem

Aspose.PDF untuk Python for .NET didukung pada semua sistem operasi utama. Pastikan Anda memiliki prasyarat berikut.

  • Microsoft® Windows™ atau OS yang kompatibel dengan .NET Framework, .NET Core, dan PHP, VBScript, Delphi, C++ melalui COM Interop.
  • Lingkungan pengembangan seperti Microsoft Visual Studio.
  • Aspose.PDF untuk .NET DLL direferensikan dalam proyek Anda.

Kode contoh ini menunjukkan PDF ke BYTEARRAY Python Konversi

    def convert_PDF_to_BYTEARRAY(self, infile, outfile):

        path_infile = self.dataDir + infile
        path_outfile = self.dataDir + outfile

        # Open PDF document

        document = Document(path_infile)

        
        print(infile + " converted into " + outfile)

  • Konversi PDF ke BYTEARRAY melalui Aplikasi Online

    Ubah PDF ke BYTEARRAY sekarang dengan mengunjungi [situs web Demo Langsung] kami (https://products.aspose.app/pdf/conversion/pdf-to-bytearray). Demo langsung memiliki manfaat sebagai berikut

      Tidak perlu mengunduh atau mengatur apa pun

      Tidak perlu menulis kode apa pun

      Klik tombol Convert. File PDF Anda akan diunggah dan akan dikonversi ke format

      Dapatkan tautan unduhan secara instan untuk file yang dihasilkan

    In this tutorial, we will learn about the Python bytearray() method with the help of examples.

    The bytearray() method returns a bytearray object which is an array of the given bytes.

    Example

    prime_numbers = [2, 3, 5, 7]
    
    

    # convert list to bytearray byte_array = bytearray(prime_numbers)

    print(byte_array) # Output: bytearray(b'\x02\x03\x05\x07')


    bytearray() Syntax

    The syntax of bytearray() method is:

    bytearray([source[, encoding[, errors]]])

    bytearray() method returns a bytearray object (i.e. array of bytes) which is mutable (can be modified) sequence of integers in the range 0 <= x < 256.

    If you want the immutable version, use the bytes() method.


    bytearray() Parameters

    bytearray() takes three optional parameters:

    • source (Optional) - source to initialize the array of bytes.
    • encoding (Optional) - if the source is a string, the encoding of the string.
    • errors (Optional) - if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)

    The source parameter can be used to initialize the byte array in the following ways:

    TypeDescription
    String Converts the string to bytes using str.encode() Must also provide encoding and optionally errors
    Integer Creates an array of provided size, all initialized to null
    Object A read-only buffer of the object will be used to initialize the byte array
    Iterable Creates an array of size equal to the iterable count and initialized to the iterable elements Must be iterable of integers between 0 <= x < 256
    No source (arguments) Creates an array of size 0.


    bytearray() Return Value

    The bytearray() method returns an array of bytes of the given size and initialization values.


    Example 1: Array of bytes from a string

    string = "Python is interesting."
    
    

    # string with encoding 'utf-8' arr = bytearray(string, 'utf-8')

    print(arr)

    Output

    bytearray(b'Python is interesting.')

    Example 2: Array of bytes of given integer size

    size = 5
    
    

    arr = bytearray(size)

    print(arr)

    Output

    bytearray(b'\x00\x00\x00\x00\x00')

    Example 3: Array of bytes from an iterable list

    rList = [1, 2, 3, 4, 5]
    
    

    arr = bytearray(rList)

    print(arr)

    Output

    bytearray(b'\x01\x02\x03\x04\x05')