- Python XlsxWriter 教程
- Python XlsxWriter - 首頁
- Python XlsxWriter - 概述
- Python XlsxWriter - 環境設定
- Python XlsxWriter - Hello World
- Python XlsxWriter - 重要類
- Python XlsxWriter - 單元格表示法和範圍
- Python XlsxWriter - 定義名稱
- Python XlsxWriter - 公式和函式
- Python XlsxWriter - 日期和時間
- Python XlsxWriter - 表格
- Python XlsxWriter - 應用篩選器
- Python XlsxWriter - 字型和顏色
- Python XlsxWriter - 數字格式
- Python XlsxWriter - 邊框
- Python XlsxWriter - 超連結
- Python XlsxWriter - 條件格式化
- Python XlsxWriter - 新增圖表
- Python XlsxWriter - 圖表格式化
- Python XlsxWriter - 圖表圖例
- Python XlsxWriter - 條形圖
- Python XlsxWriter - 折線圖
- Python XlsxWriter - 餅圖
- Python XlsxWriter - Sparklines (迷你圖)
- Python XlsxWriter - 資料驗證
- Python XlsxWriter - 大綱和分組
- Python XlsxWriter - 凍結和拆分窗格
- Python XlsxWriter - 隱藏/保護工作表
- Python XlsxWriter - 文字框
- Python XlsxWriter - 插入圖片
- Python XlsxWriter - 頁面設定
- Python XlsxWriter - 頁首和頁尾
- Python XlsxWriter - 單元格批註
- Python XlsxWriter - 使用 Pandas
- Python XlsxWriter - VBA 宏
- Python XlsxWriter 有用資源
- Python XlsxWriter - 快速指南
- Python XlsxWriter - 有用資源
- Python XlsxWriter - 討論
Python XlsxWriter - 插入圖片
藉助insert_image() 方法,可以在工作表的特定單元格位置插入影像物件。基本上,您必須使用任何型別的表示法指定單元格位置以及要插入的影像。
worksheet.insert_image('C5', 'logo.png')
insert_image() 方法在字典中接受以下可選引數。
| 引數 | 預設值 |
|---|---|
| 'x_offset' | 0, |
| 'y_offset' | 0, |
| 'x_scale' | 1, |
| 'y_scale' | 1, |
| 'object_position' | 2, |
| 'image_data' | None |
| 'url' | None |
| 'description' | None |
| 'decorative' | False |
偏移值以畫素為單位。x_scale 和 y_scale 引數用於水平和垂直縮放影像。
image_data 引數用於新增io.BytesIO 格式的記憶體位元組流。
示例
以下程式從當前資料夾中的檔案提取影像資料,並將其用作image_data 引數的值。
from io import BytesIO
import xlsxwriter
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
filename = 'logo.png'
file = open(filename, 'rb')
data = BytesIO(file.read())
file.close()
worksheet.insert_image('C5', filename, {'image_data': data})
workbook.close()
輸出
以下是結果工作表的檢視:
廣告