zhangyu836 / xltpl Goto Github PK
View Code? Open in Web Editor NEWA python module to generate xls/x files from a xls/x template.
License: MIT License
A python module to generate xls/x files from a xls/x template.
License: MIT License
with xltpl.writer (.xls) - all good.
P.S.
And something strange: PermissionError: [WinError 32] 'C:\Users\andre\AppData\Local\Temp\openpyxl.faelgi1b'
例如,有100人的信息,要填到excel文件里,但是要求
以上这样做的目的,是方便打印后观看
那么,应该怎么样设计模板,实现每30行(如果加标题,就是31行数据;其实第一页可能更复杂,比如第一行还写着“302班成绩表”,不过这一行的数据,手工添加也可以)占据打印时的一张纸?
谢谢啊
第一页的表,
学号 | 姓名 | 语文 | 数学 |
---|---|---|---|
001 | 孙悟空 | 80 | 90 |
002 | 猪八戒 | 90 | 50 |
... | ... | ... | |
030 | 唐僧 | 100 | 90 |
第4页的表
学号 | 姓名 | 语文 | 数学 |
---|---|---|---|
091 | 宋江 | 80 | 90 |
092 | 武松 | 90 | 50 |
... | ... | ... | |
100 | 孙二娘 | 100 | 90 |
感谢作者的分享,这里有个小问题:原模板有页眉页脚信息,生成的文件中没有页眉页脚,能否提供相应支持?
The image function in the example does not work.
Using python 3.12 a regex DeprecationWarning
got upgraded to SyntaxWarning
.
.../lib/python3.12/site-packages/xltpl/utils.py:23: SyntaxWarning: invalid escape sequence '\d'
COORD = '[a-z]{1,3}\d+'
.../lib/python3.12/site-packages/xltpl/utils.py:24: SyntaxWarning: invalid escape sequence '\S'
RANGETAG = '%s *%s *(%s:%s\S*?) *%s' % (RANGE, VARIABLE_START_STRING, COORD, COORD, VARIABLE_END_STRING)
.../lib/python3.12/site-packages/xltpl/utils.py:111: SyntaxWarning: invalid escape sequence '\d'
FIXTEST = '({(?:___\d+___)?(?:{|%).+?(?:}|%)(?:___\d+___)?})'
.../lib/python3.12/site-packages/xltpl/utils.py:112: SyntaxWarning: invalid escape sequence '\d'
RUNSPLIT = '(___\d+___)'
.../lib/python3.12/site-packages/xltpl/utils.py:113: SyntaxWarning: invalid escape sequence '\d'
RUNSPLIT2 = '___(\d+)___'
.../lib/python3.12/site-packages/xltpl/jinja.py:25: SyntaxWarning: invalid escape sequence '\d'
p = re.compile("'(\d*,\d*[,\d]*)'")
A backslash-character pair that is not a valid escape sequence now generates a SyntaxWarning, instead of DeprecationWarning. For example, re.compile("\d+.\d+") now emits a SyntaxWarning ("\d" is an invalid escape sequence, use raw strings for regular expression: re.compile(r"\d+.\d+")). In a future Python version, SyntaxError will eventually be raised, instead of SyntaxWarning.
from https://docs.python.org/3/whatsnew/3.12.html#other-language-changes
您好,excel模板中有logo图片的话,生成的文件中图片丢失,请问有没有解决办法。
Hello.
The library supports iterate columns like row when you use beforerows tag?
Thanks
Current version 0.19 requires openpyxl >= 2.6.0, <= 3.0.9. I need to use openpyxl-3.1.2, can xltpl be upgraded to use openpyxl-3.1.2 please.
I created an xlsx template, with column headers in the first row, and set auto filters on (Data > Filter menu in Excel). The results after rendering were correct except that the auto filter was missing.
I corrected this in the package code on my PC by copying the auto_filter setting in basex.py, SheetBase.copy_sheet_settings()
self.wtsheet.auto_filter = copy.copy(self.rdsheet.auto_filter)
This worked fine in my case, because I had filters on the first row, however it will not work properly if the result of rendering moves the position of the cells being filtered.
大神你好,工具是否支持图片写入?
我的需求是这样:用excel打印类似于快递单那样的标签,标签中内容除了文字以外还有二维码用于扫码,我用excel做好了模板,然后python代码生成了二维码图片,使用 xltpl 能正常写入文字信息,但是二维码图片不知如何写入
Hello,
I have been trying to change the background and font color of a cell with jinja2 using an if condition in the template but I cannot find any info on how to.
It seems like the {%yn } tag as a RichText capability but there is no documentation on how to use it.
I tried to use nested loop to write data in my sheet and a column is missing in the final excel output if a the nested list is empty.
Here is the code I tried with the template template.xlsx :
from xlsxtpl.writerx import BookWriter
writer = BookWriter('template.xlsx')
writer.jinja_env.globals.update(dir=dir, getattr=getattr)
# Define the data that you want to fill into the template
data = {
'persons':[
{
'name': 'John',
'children':[
],
'age': 28
},
{
'name': 'Peter',
'children':[
{'name':'Ana'},
{'name':'Eve'}
],
'age': 49
}
],
}
writer.render_book(payloads=[data])
# Create a new workbook
writer.save("out.xlsx")
This is what was generated : out.xlsx.
the expected output would be an empty B2 cell and the data "28" in the C2 cell instead of the B2 cell.
Hi,
does the package support hyperlinks created using variables?
For example I have a column where I create a string using the Excel HYPERLINK formula, something like =HYPERLINK("{{url}}","{{display_text}}") but this does not act as a hyperlink (i.e. does not open the link) when I open the rendered Excel workbook. If I edit the workbook and press Enter on each cell then it converts to a proper hyperlink.
I have a workaround where I use OpenPyXL to convert each of these cells to a hyperlink after rendering the workbook, but it would be nice if I could do this directly in xltpl.
thanks.
存在一个temp.xlsx文件,包括2个sheet(sum和list);
需要生成一个新的文件,其sheet类似于sum,list1,list2,list3.
文件有工作簿A1,A1设置一个打印区域,从A1单元格到G13单元格。使用xlsxtpl.writerx.render_book2
渲染并保存后用Microsoft Office打开该文件,会提示文件错误,如下图:
from openpyxl import load_workbook
from xlsxtpl.writerx import BookWriter
writer = BookWriter("hello.xlsx")
wb = load_workbook("hello.xlsx", rich_text=True)
sheet_list = wb.sheetnames
payloads = [{'tpl_name': sheet, 'sheet_name': sheet, 'ctx': {}} for sheet in sheet_list]
writer.render_book2(payloads=payloads)
writer.save('test3.xlsx')
从错误信息来看可知存在问题的文件是/xl/workbook.xml
,将文件后缀改成.zip
,然后打开/xl/workbook.xml
文件。
原始文件的内容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
<fileVersion appName="xl" lastEdited="6" lowestEdited="5" rupBuild="14420"/>
<workbookPr filterPrivacy="1" defaultThemeVersion="124226"/>
<bookViews>
<workbookView xWindow="240" yWindow="120" windowWidth="16155" windowHeight="8505"/>
</bookViews>
<sheets>
<sheet name="A1" sheetId="1" r:id="rId1"/>
</sheets>
<definedNames>
<definedName name="_xlnm.Print_Area" localSheetId="0">'A1'!$A$1:$G$13</definedName>
</definedNames>
<calcPr calcId="145621"/>
</workbook>
这里需要重点关注这一段:
<definedNames>
<definedName name="_xlnm.Print_Area" localSheetId="0">'A1'!$A$1:$G$13</definedName>
</definedNames>
这里打印区域的值是:'A1'!$A$1:$G$13
。
然后查看渲染后生成的文件的 xml 设置:
<definedNames>
<definedName name="_xlnm.Print_Area" localSheetId="0">A1:G13</definedName>
</definedNames>
这里打印区域的值是:A1:G13
。
两个值不一样,转换后的文件打印区域的值不完整
需要将所有工作簿的打印区域重新设置一次再保存,才能将这个值设置为正确格式,openpyxl会帮我们修正这个格式。
...
writer.render_book2(payloads=payloads)
for sheet in writer.workbook.worksheets:
sheet.print_area = sheet.print_area
writer.save('test3.xlsx')
Would it be possible to implement support for notes/comments on cells?
As it is now, it deletes all existing notes/comments from the sheet.
Ideally it would render the them as well and populate jinja keywords with values.
大佬好,如题,例如:
原模版文件中,一些单元格包含数据验证,如何在输出文件中保留这一点,谢谢
openpyxl中指的是data validation可以读写,理论上应该可以做到,希望大佬能支持 谢谢!
大佬好 使用中 当页眉页脚是图片类型的话不能正常导出 文字正常
excel模板
A1批注:aftercell {% for row in soRows %}
B1列内容:{{row.Order_Date}}
C1批注:beforecell {% endfor %}
生成的列内容会全部挤在B1里面
如果我想生成列,我的在C1前面多加一列。但是这样生成出来会多很多列
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.