Python 之 Excel 截圖


Python 之 Excel 截圖

win32 截圖

# win32 截圖
def winPic(excel_path):
    # file='截圖.xlsx'
    # file_name = os.path.abspath(file)  # 把相對路徑轉成絕對路徑
    pythoncom.CoInitialize()  # 開啟多線程
    # 創建Excel對象
    excel = DispatchEx('excel.application')
    excel.visible = False         # 不顯示Excel
    excel.DisplayAlerts = 0     # 關閉系統警告(保存時不會彈出窗口)
    # excel.ScreenUpdating = 1    # 關閉屏幕刷新

    workbook = excel.workbooks.Open(excel_path)  # 打開Excel文件
    sheet = workbook.worksheets['截圖']
    img_name = '截圖'
    screen_area = sheet.UsedRange  # 有內容的區域
    screen_area.CopyPicture()  # 復制圖片區域
    sheet.Paste()  # 粘貼
    excel.Selection.ShapeRange.Name = img_name  # 將剛剛選擇的Shape重命名,避免與已有圖片混淆
    sheet.Shapes(img_name).Copy()  # 選擇圖片
    img = ImageGrab.grabclipboard()  # 獲取剪貼板的圖片數據
    # time.sleep(2)
    print(img)  # 可以弄個報錯
    img.save(img_name + ".png")
    workbook.Close(False)  # 關閉Excel文件,不保存
    excel.Quit()  # 退出Excel
    pythoncom.CoUninitialize()  # 關閉多線程

Excel xlwings制作圖片

# Excel xlwings制作圖片
def xlwingsPic(excel_path, name):
    print('分析數據:'+name)
    app = xw.App(visible=True, add_book=False)
    # 1. 使用 xlwings 的 讀取 path 文件 啟動
    wb = app.books.open(excel_path)
    # 2. 讀取 sheet
    sht = wb.sheets[0]
    # 3. 獲取 行與列
    nrow = sht.used_range.last_cell.row
    ncol = sht.used_range.last_cell.column
    # 自動調整單元格大小。
    sht.autofit()
    # 合並‘合計’單元格
    Brow = 'C' + str(nrow)
    Hrow = 'I' + str(nrow)
    mer = Brow+':'+Hrow
    sht.range(mer).api.Merge()
    heji = sht.range(Hrow)

    # 4. 獲取有內容的 range
    range_val = sht.range(
        (1, 3),  # 獲取 第一行 第一列
        (nrow, ncol-1)  # 獲取 第 nrow 行 第 ncol 列
    )
    # 設置背景色
    range_val.color = (255, 255, 255)
    # 設置單元格的對齊方式
    range_val.api.HorizontalAlignment = -4108    # -4108 水平居中。 -4131 靠左,-4152 靠右。
    # range_val.api.VerticalAlignment = -4130      # -4108 垂直居中(默認)。 -4160 靠上,-4107 靠下, -4130 自動換行對齊。

    # ‘合計’右對齊
    heji.api.HorizontalAlignment = -4152

    # Borders(9) 底部邊框,LineStyle = 1 直線。2 虛線。 5 雙點划線。4 點划線。
    range_val.api.Borders(9).LineStyle = 1
    range_val.api.Borders(9).Weight = 2               # 設置邊框粗細。

    # Borders(7) 左邊框
    range_val.api.Borders(7).LineStyle = 1
    range_val.api.Borders(7).Weight = 2

    # Borders(8) 頂部框
    range_val.api.Borders(8).LineStyle = 1
    range_val.api.Borders(8).Weight = 2

    # Borders(10) 右邊框
    range_val.api.Borders(10).LineStyle = 1
    range_val.api.Borders(8).Weight = 2

    # 區域的單元格,內部邊框
    # # Borders(11) 內部垂直邊線。
    range_val.api.Borders(11).LineStyle = 1
    range_val.api.Borders(11).Weight = 2
    # # Borders(12) 內部水平邊線。
    range_val.api.Borders(12).LineStyle = 1
    range_val.api.Borders(12).Weight = 2

    # 5. 復制圖片區域
    range_val.api.CopyPicture()
    time.sleep(0.5)
    # 6. 粘貼
    sht.api.Paste()
    pic = sht.pictures[0]  # 當前圖片
    pic.api.Copy()

    time.sleep(0.5)
    # 獲取剪貼板的圖片數據
    im = ImageGrab.grabclipboard()
    time.sleep(5)
    riqi = str(time.strftime("%Y-%m-%d", time.localtime()))
    ImageAdd = 'D:\\AutoSend\\Image\\' + str(riqi + name) + '.png'

    # 判斷圖片是否正確
    if isinstance(im, Image.Image):
        # print ("Image: size : %s, mode: %s" % (im.size, im.mode))
        im.save(ImageAdd)
    elif im:
        for filename in im:
            try:
                print("filename: %s" % filename)
                im = Image.open(filename)
            except IOError:
                pass  # ignore this file
            else:
                print("ImageList: size : %s, mode: %s") % (im.size, im.mode)
    else:
        print("clipboard is empty.")

    pic.delete()  # 刪除sheet上的圖片

    wb.close()  # 不保存,直接關閉
    app.quit()  # 退出
    # 返回圖片保存地址
    return ImageAdd


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM