[英]Python Excel template read and re-write, maintaining formulae and formatting
我已經運行了全部范圍,似乎無法找到我正在尋找的東西。 我在這里找到的所有主題對我來說都是死路一條。 xlrd,xlwt和xlutils幾乎可以滿足我的需要,但是......基本思想是我需要使用Python將簡單數據(字符串)寫入特定的Excel模板,並將該模板寫入新的.xls文件。
讀取模板,將其復制到新的工作簿對象,寫入新值,並寫出新的.xls文件是沒有問題的。 但是,我找不到維持兩個公式和格式化一個Python的Excel模塊。
`
our = 'template.xls'
# open up read-only template with formatting maintained
rb = open_workbook(our,formatting_info=True)
r_sheet = rb.sheet_by_index(4)
# copy this to a new workbook object
wb = copy(rb)
# 5th sheet (index = 4) is where we need to drop the data we have
w_sheet = wb.get_sheet(4)
# populate with new data!
for row_index in range(0, r_sheet.nrows):
w_sheet.write(row_index, 1, "some string that is our data")
# write it!
wb.save('new.xls')
`
在模板中讀取時,格式保持不變(對於某些顏色略有改變; meh!),但公式不是,因此“new.xls”在公式曾經存在的地方都有空白。
formatting and formulae? 任何想法如何保持的格式和公式?
注意,我已經鎖定了Python 2.7。
你可以使用openpyxl
- 它只支持xlsx(及相關格式),而不支持xls(舊的Excel格式)。
https://pypi.python.org/pypi/openpyxl/
http://openpyxl.readthedocs.org/en/latest/
https://bitbucket.org/openpyxl/openpyxl/src/2.2/doc/source/index.rst
打開/保存Excel工作表時,此包會保留公式。 它也確實保留了格式,但它有一些小問題。 但是,不保留原始Excel文件中的圖像。
其他很酷的功能包括支持條件格式,圖表,迷你圖等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.