Skip to content

Document

Excel, PDF, and Word document read/write/convert.

8 modules

ModuleDescription
讀取 Excel從 Excel 檔案(xlsx、xls)讀取資料
寫入 Excel將資料寫入 Excel 檔案(xlsx)
填寫 PDF 表單填寫 PDF 表單欄位並可選擇插入圖片
產生 PDF從 HTML 內容或文字產生 PDF 檔案
解析 PDF從 PDF 檔案擷取文字和中繼資料
PDF 轉 Word將 PDF 檔案轉換為 Word 文件(.docx)
解析 Word 文件從 Word 文件(.docx)擷取文字和內容
Word 轉 PDF將 Word 文件(.docx)轉換為 PDF 檔案

Modules

讀取 Excel

excel.read

從 Excel 檔案(xlsx、xls)讀取資料

Parameters:

NameTypeRequiredDefaultDescription
pathstringYes-Path to the Excel file
sheetstringNo-Sheet name (default: first sheet)
header_rownumberNo1Row number for headers (1-based, 0 for no headers)
rangestringNo-Cell range to read (e.g., "A1:D10")
as_dictbooleanNoTrueReturn rows as dictionaries (using headers as keys)

Output:

FieldTypeDescription
dataarray擷取的資料列
headersarray擷取的資料列
row_countnumber擷取的資料列
sheet_namesarray欄位標頭

Example: Read entire sheet

yaml
path: /tmp/data.xlsx
as_dict: true

寫入 Excel

excel.write

將資料寫入 Excel 檔案(xlsx)

Parameters:

NameTypeRequiredDefaultDescription
pathstringYes-Path to the Excel file
dataarrayYes-Data to write (array of arrays or array of objects)
headersarrayNo-Column headers (auto-detected from objects if not provided)
sheet_namestringNoSheet1Name of the worksheet
auto_widthbooleanNoTrueAutomatically adjust column widths

Output:

FieldTypeDescription
pathstring建立的 Excel 檔案路徑
row_countnumber建立的 Excel 檔案路徑
sizenumber建立的 Excel 檔案路徑

Example: Write data to Excel

yaml
path: /tmp/output.xlsx
data: [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

填寫 PDF 表單

pdf.fill_form

填寫 PDF 表單欄位並可選擇插入圖片

Parameters:

NameTypeRequiredDefaultDescription
templatestringYes-Path to the PDF template file
outputstringYes-Path for the output document
fieldsobjectNo{}Key-value pairs of form field names and values
imagesarrayNo[]List of images to insert with position info
flattenbooleanNoTrueFlatten form fields (make them non-editable)

Output:

FieldTypeDescription
output_pathstring已填寫 PDF 的路徑
fields_fillednumber已填寫 PDF 的路徑
images_insertednumber已填寫 PDF 的路徑
file_size_bytesnumber插入的圖片數量

Example: Fill form with text fields

yaml
template: /templates/form.pdf
output: /output/filled.pdf
fields: {"name": "John Doe", "id_number": "A123456789", "date": "2024-01-01"}

Example: Fill form with photo

yaml
template: /templates/id_card.pdf
output: /output/id_card_filled.pdf
fields: {"name": "Jane Doe"}
images: [{"file": "/photos/jane.jpg", "page": 1, "x": 50, "y": 650, "width": 100, "height": 120}]

產生 PDF

pdf.generate

從 HTML 內容或文字產生 PDF 檔案

Parameters:

NameTypeRequiredDefaultDescription
contentstringYes-HTML or text content to convert to PDF
output_pathstringYes-Path for the output document
titlestringNo-Document title (metadata)
authorstringNo-Document author (metadata)
page_sizeselect (A4, Letter, Legal, A3, A5)NoA4Page size format
orientationselect (portrait, landscape)NoportraitPage orientation
marginnumberNo20Page margin in millimeters
headerstringNo-Header text for each page
footerstringNo-Footer text for each page

Output:

FieldTypeDescription
output_pathstring產生的 PDF 路徑
page_countnumber產生的 PDF 路徑
file_size_bytesnumberPDF 的頁數

Example: Generate from HTML

yaml
content: <h1>Report</h1><p>Content here</p>
output_path: /path/to/report.pdf
title: Monthly Report

解析 PDF

pdf.parse

從 PDF 檔案擷取文字和中繼資料

Parameters:

NameTypeRequiredDefaultDescription
pathstringYes-Path to the PDF file
pagesstringNoallPage range (e.g., "1-5", "1,3,5", or "all")
extract_imagesbooleanNoFalseExtract embedded images
extract_tablesbooleanNoFalseExtract tables as structured data

Output:

FieldTypeDescription
textstring擷取的文字內容
pagesarray擷取的文字內容
metadataobject擷取的文字內容
page_countnumber每頁的文字內容

Example: Extract all text from PDF

yaml
path: /tmp/document.pdf
pages: all

PDF 轉 Word

pdf.to_word

將 PDF 檔案轉換為 Word 文件(.docx)

Parameters:

NameTypeRequiredDefaultDescription
input_pathstringYes-Path to the input document
output_pathstringNo-Path for the output document
preserve_formattingbooleanNoTruePreserve basic formatting
pagesstringNoallPage range (e.g., "1-5", "1,3,5", or "all")

Output:

FieldTypeDescription
output_pathstring產生的 Word 文件路徑
page_countnumber產生的 Word 文件路徑
file_sizenumber轉換的頁數

Example: Convert entire PDF to Word

yaml
input_path: /tmp/document.pdf

Example: Convert specific pages

yaml
input_path: /tmp/document.pdf
output_path: /tmp/output.docx
pages: 1-5

解析 Word 文件

word.parse

從 Word 文件(.docx)擷取文字和內容

Parameters:

NameTypeRequiredDefaultDescription
file_pathstringYes-Path to the Word document (.docx)
extract_tablesbooleanNoTrueExtract tables as structured data
extract_imagesbooleanNoFalseExtract embedded images
images_output_dirstringNo-Directory to save extracted images
preserve_formattingbooleanNoFalsePreserve basic formatting

Output:

FieldTypeDescription
textstring文件的完整文字內容
paragraphsarray文件的完整文字內容
tablesarray文件的完整文字內容
imagesarray段落列表
metadataobject擷取的表格(陣列格式)

Example: Extract text from Word

yaml
file_path: /path/to/document.docx

Example: Extract with tables and images

yaml
file_path: /path/to/document.docx
extract_tables: true
extract_images: true
images_output_dir: /path/to/images/

Word 轉 PDF

word.to_pdf

將 Word 文件(.docx)轉換為 PDF 檔案

Parameters:

NameTypeRequiredDefaultDescription
input_pathstringYes-Path to the input document
output_pathstringNo-Path for the output document
methodselect (auto, libreoffice, docx2pdf)NoautoMethod to use for conversion

Output:

FieldTypeDescription
output_pathstring產生的 PDF 檔案路徑
file_sizenumber產生的 PDF 檔案路徑
method_usedstring輸出檔案大小(位元組)

Example: Convert Word to PDF

yaml
input_path: /tmp/document.docx

Example: Convert with specific output path

yaml
input_path: /tmp/document.docx
output_path: /tmp/output.pdf

Released under the Apache 2.0 License.