如何生成可打印的 PDF 文件(美国信函大小),使每个页面代表一个月并进行分区,以便该月的每一天都有一个相同大小的框?如果我想跳过周末而只显示工作日怎么办?
我将使用哪些 Python 模块来完成以下任务?:
- 生成具有美国信件分辨率的图像
- 迭代该月的每一天,并可以选择跳过特定日期(例如所有周末)
- 分区图像,使得该月的每一天都列在固定大小的框中
- 对给定一年中的所有月份重复步骤 2-3
- 生成 pdf 作为输出
How can I produce a printable PDF file (US letter sized) such that each page represents a month and is partitioned such that each day of the month gets a box of equal size? What if I want to skip weekends and just display weekdays?
What Python modules would I use to accomplish the following?:
- Producing an image with the resolution of a US letter
- Iterating through each day of the month with the option to skip specific days (e.g., all weekends)
- Partitioning the image such that each day of the month is listed in a box of fixed size
- Repeating steps 2-3 for all months in a given year
- Producing a pdf as the output
发布评论
评论(3)
你可以用 3 个包来完成。 “Reportlab”用于生成 pdf,“calendar”用于获取月份列表,以及“Ghostscript”的 python 绑定用于将生成的 pdf 转换为 png。
您将首先从日历包中获取数据,使用 Reportlab 生成美国信函大小的页面。可以对表格进行操作,使其具有网格,使每个单元格具有相同大小的框,并更改文本字体、大小和对齐方式。
如果您只想要 pdf,则可以保留它,或者您可以使用 Ghostscript python 绑定将此 pdf 转换为图像。或者,如果您愿意,可以使用 system('gs ...') 运行“Ghostscript”。此外,必须安装 Ghostscript 才能使 python 'Ghostscript' 包正常工作。
如果您想过滤掉所有周末,那么您可以对日历数据使用良好的老式列表操作。
以下是如何生成 pdf 的示例。我不会只用一个月来计算一整年,而且我也不会费心过滤掉零。
如果您想要另一个页面,请添加 PageBreak(),然后将下一个日历添加到传递给 doc.build() 的列表中。 PageBreak 是reportlab.platypus 的一部分。
要将 pdf 转换为 png,
reportlab 和 Ghostscript 软件包都可以通过使用 pip 获得。我在“virtualenv”环境中创建了上述内容。
报告实验室
http://www.reportlab.com/software/opensource/rl-toolkit/
Ghostscript python 绑定
https://bitbucket.org/htgoebel/python-ghostscript
日历是标准 python 库的一部分。
You could do it with 3 packages. 'Reportlab' for producing the pdf, 'calendar' for getting the month as lists of lists, and python binding for 'Ghostscript' to transform the pdf produced into a png.
You would start by getting the data from the calendar package, using Reportlab to produce a page of US letter size. The table can be manipulated to have a grid, have each cell a box of the same size and alter the text font, size, and alignment.
You could leave it at that if you just want a pdf, or you can convert this pdf into a image using Ghostscript python bindings. Or if you like you can just run 'Ghostscript' using system('gs ...'). Also Ghostscript must be installed for the python 'Ghostscript' package to work.
If you want to filter out all weekends then you can use good old fashioned list manipulation on the calendar data for that.
Here is a example of how you could produce the pdf. I'm not going to do a whole year just a single month, and I'm not going to bother filtering out the zeros.
If you want another page add PageBreak() followed by the next calendar to the list passed to doc.build(). PageBreak is part of reportlab.platypus.
And to convert the pdf to png
Both reportlab and ghostscript packages are available through the use of pip. I created the above in a 'virtualenv' environment.
ReportLab
http://www.reportlab.com/software/opensource/rl-toolkit/
Ghostscript python bindings
https://bitbucket.org/htgoebel/python-ghostscript
calendar is part of the standard python library.
对于任何离开 Google 的人,一位名叫 Bill Mill 的人编写了一个
公共领域模块这使得使用 reportlab 生成日历就像此示例文本一样简单。我提供的链接中还有示例输出,虽然它简单且简洁,但看起来不错(类似于您从 Scribus 的“制作日历”脚本中获得的结果),并且将为未来的增强功能提供一个极好的起点。
完整代码:
编辑2017-11-25:我必须重构它以供自己使用,所以我想我应该在这里分享它。最新版本将始终在此 GitHub Gist 中,但在下面,我包含了最后一个版本在获得对 PyEphem 的依赖来计算月相等内容之前进行了修订:
重构的代码具有以下优点:
。 更新 2021-09-30: 这是最后一个代码块生成的
calendar.pdf
在 Okular 1.9.3 中以 75% 的分辨率查看时的样子:(忽略不同的线宽。这只是 Okular 1.9.3 渲染错误,会在某些缩放级别出现。)
For anyone who wanders in off Google, a fellow named Bill Mill wrote a
public domain modulethat makes generating a calendar using reportlab as simple as this example text.There's also sample output at the link I provided and, while it's simple and spartan, it looks decent (similar to what you get out of things like the "make calendar" script for Scribus) and would make an excellent starting point for future enhancements.
Full code:
EDIT 2017-11-25: I had to refactor this for my own use, so I thought I'd share it here. The newest version will always be in this GitHub Gist but, below, I'm including the last revision before it gained a dependency on PyEphem for calculating things like moon phases:
The refactored code has the following advantages:
UPDATE 2021-09-30: Here's what the
calendar.pdf
generated by that last code block looks like when viewed in Okular 1.9.3 at 75%:(Ignore the varying line widths. That's just an Okular 1.9.3 rendering bug that shows up at certain zoom levels.)
不久前我遇到了类似的问题 - 我使用了出色的 pcal 实用程序。它不是 python,但即使作为一个 python 偏执者,我也发现从 python 获取可靠的可打印 PDF 存在严重限制 - 我的 LaTeX 不够好
http://www.itmanagerscookbook.com/Workstation/power-user/calendar.html
I had a similar roblem a while back - I used the excellent pcal utility. It's not python but even as a python bigot I found severe limitations getting reliable printable PDFs from python - my LaTeX was not good enough
http://www.itmanagerscookbook.com/Workstation/power-user/calendar.html