from openpyxl import load_workbook
wb = load_workbook('file.xlsx')
ws = wb['Sheet1']
有没有办法检索表示
ws
对象的xml代码?
注意:我想避免使用 zipfile
模块。相反,我试图从 ws
.中提取 xmldirectly
我阅读了
openpyxl
源代码并且正在玩 lxml
- 但没有成功。
我自己想通了。您可以在保存工作簿时捕获它,而不是通过解压缩已保存的工作簿来提取 xml。
wb.save
方法使用了 ExcelWriter
类,我为此目的修改了该类:
import openpyxl
from openpyxl.writer.excel import *
class MyExcelWriter(openpyxl.writer.excel.ExcelWriter):
def write_worksheet(self, ws):
ws._drawing = SpreadsheetDrawing()
ws._drawing.charts = ws._charts
ws._drawing.images = ws._images
if self.workbook.write_only:
if not ws.closed:
ws.close()
writer = ws._writer
else:
writer = WorksheetWriter(ws)
writer.write()
ws._rels = writer._rels
# my addition starts here
if ws.title == 'My Sheet':
with open(writer.out, 'r', encoding='utf8') as file:
xml_code = file.read()
# my code continues here...
# my addition ends here
self._archive.write(writer.out, ws.path[1:])
self.manifest.append(ws)
writer.cleanup()
openpyxl.writer.excel.ExcelWriter = MyExcelWriter
write_worksheet
函数创建一个临时xml文件,其路径存放在writer.out
.from <module> import *
被认为是不好的做法——谨慎使用。