GithubHelp home page GithubHelp logo

zhangyu836 / xltpl Goto Github PK

View Code? Open in Web Editor NEW
71.0 4.0 17.0 487 KB

A python module to generate xls/x files from a xls/x template.

License: MIT License

Python 100.00%
xls excel template openpyxl xlsx spreadsheet spreadsheet-manipulation jinja2 python

xltpl's People

Contributors

zhangyu836 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xltpl's Issues

页眉页脚丢失

感谢作者的分享,这里有个小问题:原模板有页眉页脚信息,生成的文件中没有页眉页脚,能否提供相应支持?

请问如何设计模板,让填充的表格等份、出现在多页上

例如,有100人的信息,要填到excel文件里,但是要求

  1. 每一页最多只放30人的数据,
  2. 每一页的表格并不占满纸张,而是在这一页的表格下方有若干空行

以上这样做的目的,是方便打印后观看

那么,应该怎么样设计模板,实现每30行(如果加标题,就是31行数据;其实第一页可能更复杂,比如第一行还写着“302班成绩表”,不过这一行的数据,手工添加也可以)占据打印时的一张纸?
谢谢啊

第一页的表,

学号 姓名 语文 数学
001 孙悟空 80 90
002 猪八戒 90 50
... ... ...
030 唐僧 100 90

第4页的表

学号 姓名 语文 数学
091 宋江 80 90
092 武松 90 50
... ... ...
100 孙二娘 100 90

Support for notes/comments in cell

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打印类似于快递单那样的标签,标签中内容除了文字以外还有二维码用于扫码,我用excel做好了模板,然后python代码生成了二维码图片,使用 xltpl 能正常写入文字信息,但是二维码图片不知如何写入

Column not written when nested list is empty

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.

列的生成,可能有bug

excel模板
A1批注:aftercell {% for row in soRows %}
B1列内容:{{row.Order_Date}}
C1批注:beforecell {% endfor %}
生成的列内容会全部挤在B1里面

如果我想生成列,我的在C1前面多加一列。但是这样生成出来会多很多列

image

The image function in the example does not work.

如何自动合并单元格

你好 这有一个数据:

data = {
    'yplb':[
        {
            'lx': 'A',
            'bh':[
                  'A01',
                  'A02',
                  'A03'
            ],
            'xm': 'XMA01'
        },
        {
            'lx': 'B',
            'bh':[
                  'B01',
                  'B02'
            ],
            'xm': 'XMB01'
        },
    ],
   
}

如何可以生成如下的样式:
image

Auto filters not included in the output after rendering

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.

Regex SyntaxWarning

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

带有打印区域的文件在生成后用MS Office打开提示文件错误

现象

文件有工作簿A1,A1设置一个打印区域,从A1单元格到G13单元格。使用xlsxtpl.writerx.render_book2渲染并保存后用Microsoft Office打开该文件,会提示文件错误,如下图:

image
image

文件

原始文件
渲染后的文件

代码

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')

Add documentation for possible values and what they do

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.

Hyperlinks

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.