實用又好用,6 款 Python 特殊文本格式處理庫推薦

以下是一些 Python 編寫的用來解析和操作特殊文本格式的庫,希望對大家有所幫助。

實用又好用,6 款 Python 特殊文本格式處理庫推薦


1、Tablib

Tablib 是一個用來處理與表格格式數據有關的 Python 庫,允許導入、導出、管理表格格式數據,並具備包括切片、動態列、標籤和過濾,以及格式化導入和導出等高級功能。

Tablib 支持導出/導入的格式包括:Excel 、JSON 、YAML 、HTML 、TSV 和 CSV ,暫不支持 XML 。

<code>>>> data = tablib.Dataset(headers=['First Name', 'Last Name', 'Age'])
>>> for i in [('Kenneth', 'Reitz', 22), ('Bessie', 'Monke', 21)]:
... data.append(i)


>>> print(data.export('json'))
[{"Last Name": "Reitz", "First Name": "Kenneth", "Age": 22}, {"Last Name": "Monke", "First Name": "Bessie", "Age": 21}]

>>> print(data.export('yaml'))
- {Age: 22, First Name: Kenneth, Last Name: Reitz}
- {Age: 21, First Name: Bessie, Last Name: Monke}

>>> data.export('xlsx')
<censored>

>>> data.export('df')
First Name Last Name Age
0 Kenneth Reitz 22
1 Bessie Monke 21/<censored>/<code>

2、Openpyxl

Openpyxl 是一個用於讀寫 Excel 2010 xlsx / xlsm / xltx / xltm 文件的 Python 庫。

Openpyxl 為 Python 原生讀取/寫入 Office Open XML 格式而生,最初是基於 PHPExcel 而開發。

<code>from openpyxl import Workbook 

wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("sample.xlsx")/<code>

3、unoconv

unoconv,全稱為 Universal Office Converter ,是一個命令行工具,可在 LibreOffice/OpenOffice 支持的任意文件格式之間進行轉換。

unoconv 支持批量轉換文檔,還可以結合 asciidoc和 docbook2odf / xhtml2odt 來創建 PDF 或 Word(.doc) 文件。

<code>[dag@moria cv]$ make odt pdf html doc
rm -f *.{odt,pdf,html,doc}
asciidoc -b docbook -d article -o resume.xml resume.txt
docbook2odf -f --params generate.meta=0 -o resume.tmp.odt resume.xml
Saved resume.tmp.odt
unoconv -f odt -t template.ott -o resume.odt resume.tmp.odt
unoconv -f pdf -t template.ott -o resume.pdf resume.odt
unoconv -f html -t template.ott -o resume.html resume.odt
unoconv -f doc -t template.ott -o resume.doc resume.odt/<code>

4、PyPDF2

PyPDF2 是一個純 Python PDF 庫,能夠分割、合併、裁剪和轉換 PDF 文件頁面。它還可以添加自定義數據、查看選項和密碼到 PDF 文件。

PyPDF2 可以從 PDF 中檢索文本和元數據,也可以將整個文件合併在一起。

<code>from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input1 = PdfFileReader(open("document1.pdf", "rb"))

# print how many pages input1 has:
print "document1.pdf has %d pages." % input1.getNumPages()

# add page 1 from input1 to output document, unchanged
output.addPage(input1.getPage(0))

# add page 2 from input1, but rotated clockwise 90 degrees
output.addPage(input1.getPage(1).rotateClockwise(90))

# add page 3 from input1, rotated the other way:
output.addPage(input1.getPage(2).rotateCounterClockwise(90))
# alt: output.addPage(input1.getPage(2).rotateClockwise(270))

# add page 4 from input1, but first add a watermark from another PDF:
page4 = input1.getPage(3)
watermark = PdfFileReader(open("watermark.pdf", "rb"))
page4.mergePage(watermark.getPage(0))
output.addPage(page4)


# add page 5 from input1, but crop it to half size:
page5 = input1.getPage(4)
page5.mediaBox.upperRight = (
page5.mediaBox.getUpperRight_x() / 2,
page5.mediaBox.getUpperRight_y() / 2
)
output.addPage(page5)

# add some Javascript to launch the print window on opening this PDF.
# the password dialog may prevent the print dialog from being shown,
# comment the the encription lines, if that's the case, to try this out
output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

# encrypt your new PDF and add a password
password = "secret"
output.encrypt(password)

# finally, write "output" to document-output.pdf
outputStream = file("PyPDF2-output.pdf", "wb")
output.write(outputStream)/<code>

5、Mistune

Mistune 是一個純 Python 實現的 Markdown 解析器,功能齊全,包括表格、註釋、代碼塊等。

Mistune 據稱是所有純 Python markdown 解析器中速度最快的(基準測試結果)。它在設計時考慮了模塊化,以提供一個清晰易用的可擴展的 API 。

<code>import mistune

mistune.markdown('I am using **mistune markdown parser**')
# output:

I am using mistune markdown parser

/<code>

6、csvkit

csvkit 號稱是處理 csv 文件的瑞士軍刀,集成了 csvlook , csvcut 和 csvsql 等實用工具,可以以表格形式顯示 CSV 文件,輕鬆選取 CSV 指定列,以及在其上執行 SQL 操作。

csvkit 是一個命令行工具,靈感來自 pdftk 、gdal 和其它類似工具。

實用又好用,6 款 Python 特殊文本格式處理庫推薦


分享到:


相關文章: