Python提取Excel ChartSheet图表、复选框状态及多格式文件内容技术问询
解决方案
1. 提取Excel ChartSheet中的图表
针对 .xlsx 文件(openpyxl)
openpyxl可直接识别ChartSheet对象,支持导出图表为图片,也能解析图表的数据源信息:
from openpyxl import load_workbook wb = load_workbook("example.xlsx", read_only=False) # 遍历所有工作表,筛选ChartSheet for sheet_name in wb.sheetnames: sheet = wb[sheet_name] if sheet.sheet_type == "chart": # 导出图表为图片文件 sheet.chart.export(f"{sheet_name}_chart.png") # 获取图表基础属性 print(f"图表类型: {sheet.chart.type}") # 解析图表系列数据(以常见图表类型为例) if hasattr(sheet.chart, "series"): for series in sheet.chart.series: print(f"系列名称: {series.title}") print(f"数据引用范围: {series.values}") wb.close()
针对 .xls 文件(win32com.client)
旧版xls文件需依赖Windows系统的Excel COM接口,通过win32com操作:
import win32com.client as win32 excel = win32.gencache.EnsureDispatch("Excel.Application") excel.Visible = False wb = excel.Workbooks.Open("example.xls") for sheet in wb.Sheets: if sheet.Type == 3: # 3代表ChartSheet类型 # 导出图表为图片 sheet.Export(f"{sheet.Name}_chart.png") # 读取图表数据源 chart = sheet.ChartObjects(1).Chart for series in chart.SeriesCollection(): print(f"系列名称: {series.Name}") print(f"数据公式: {series.Formula}") wb.Close(SaveChanges=False) excel.Quit()
2. 读取Excel复选框的状态与标签
针对 .xlsx 文件(openpyxl)
通过遍历工作表的shapes集合,筛选出复选框控件:
from openpyxl import load_workbook wb = load_workbook("example.xlsx") ws = wb["Sheet1"] for shape in ws.shapes: if shape.shapeType == "checkbox": # 获取复选框标签文本 label = shape.text # 获取勾选状态(布尔值) is_checked = shape.checked print(f"复选框标签: {label}, 状态: {'已勾选' if is_checked else '未勾选'}") wb.close()
针对 .xls 文件(win32com.client)
同样通过COM接口遍历Shapes集合,识别复选框控件:
import win32com.client as win32 excel = win32.gencache.EnsureDispatch("Excel.Application") excel.Visible = False wb = excel.Workbooks.Open("example.xls") ws = wb.Sheets("Sheet1") for shape in ws.Shapes: if shape.Type == 12: # 12代表CheckBox控件类型 label = shape.Caption # Value=1表示已勾选,-4146表示未勾选 is_checked = shape.Value == 1 print(f"复选框标签: {label}, 状态: {'已勾选' if is_checked else '未勾选'}") wb.Close(SaveChanges=False) excel.Quit()
3. 多格式文件内容抓取的统一方案
自定义封装工具类
推荐根据文件格式映射对应处理库,自己封装统一读取工具,避免单一库的局限性:
.xlsx:openpyxl(处理内容、图表、控件).xls:win32com/xlwings(兼容旧格式复杂元素).docx:python-docx;.doc:pywin32.pdf:pdfplumber(提取文本、表格).csv:Python内置csv模块
示例框架:
import os from openpyxl import load_workbook import pdfplumber import csv class FileExtractor: @staticmethod def extract(file_path): ext = os.path.splitext(file_path)[1].lower() if ext in [".xlsx", ".xls"]: return FileExtractor._extract_excel(file_path) elif ext == ".pdf": return FileExtractor._extract_pdf(file_path) elif ext == ".csv": return FileExtractor._extract_csv(file_path) # 其他格式可自行扩展处理逻辑 @staticmethod def _extract_excel(file_path): # 整合上述Excel内容、图表、控件的读取逻辑,返回结构化数据 pass @staticmethod def _extract_pdf(file_path): with pdfplumber.open(file_path) as pdf: text = "\n".join([page.extract_text() for page in pdf.pages]) return {"text": text} @staticmethod def _extract_csv(file_path): data = [] with open(file_path, "r", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: data.append(row) return {"rows": data}
可选的统一工具包
- textract:支持多数常见格式,但依赖外部工具(如pdftotext、antiword),安装配置较繁琐
- unstructured:专注非结构化数据提取,支持多格式,对复杂布局文件处理效果较好,适合批量场景
内容的提问来源于stack exchange,提问作者Ilian A2Z
相关产品推荐
相关产品推荐

